请帮我解决下面的问题。 [英] Please help me solve below issue.

查看:67
本文介绍了请帮我解决下面的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public int m;



  for (m = < span class =code-digit> 1 ; m< 4 ;; m ++)
{

}





现在根据 m 的值,我需要以下循环运行这么多次。

我想说:如果m = 1,然后下面的循环应该运行10次1.

如果m = 2那么下面的循环应该运行10次2(这将是20)。

 for(int i = 10; i< 50 ;; i ++)
{

}





我尝试了什么:



有谁能告诉我怎么做... ??



谢谢。

解决方案

  for (m =  1 ; m< 4; m ++)
{
for int i = 0 ; i< m * 10; i ++)
{
// 您的代码
}
}


你做for循环的方式让我觉得你可能听不懂语法。如果是这样,请告诉我,我会为你澄清。



你需要做的就是将一个for循环嵌入另一个。程序运行完成后,请查看i和totalcounter的值。当m运行2次时,我运行的总次数为20次。



  for  int  m =  0 ; m <   2 ; m ++)
{
for int i = 0 ; i < 10 ; i ++)
{
Console.WriteLine( 我算:{0},i);
Console.WriteLine( totalcounter count:{0},totalcounter);
totalcounter + = 1 ;
}
}





样品输出



我数: 0  
totalcounter count: 1
i count: 1
totalcounter count: 2
i count: 2
totalcounter count: 3
i count: 3
总计数: 4
i count: 4
总计数: 5
i count: 5
totalcounter count:< span class =code-digit> 6
i count: 6
totalcounter count: 7
i count: 7
totalcounter count: 8
i count: 8
总计数: 9
i count: 9
totalcounter count:< span class =code-digit> 10
i count: 0
totalcounter count: 11
i count: 1
totalcounter count: 12
i count: 2
totalcounter count: 13
i count:< span class =code-digit> 3
totalcounter count: 14
i count: 4
总计数: 15
i count: 5
总计数: 16
i count: 6
totalcounter count: 17
i count: 7
totalcounter count: 18
i count: 8
totalcounter count: 19
i count: 9
totalcounter count: 20


选项1:

  int  m; 
for (m = 1 ; m < ; 4 ; m ++)
{
for int j = 1 ; j < = m * 10 ; j ++)
{
for int i = 10 ; i < 50 ; i ++)
{
// 做好工作。
}
}
}







选项2:

  void  DoWork()
{
int m;

for (m = 1 ; m < 4 ; m ++)
{
流程(m);
}
}

void 流程( int 乘数)
{
for int count = 1 ; count < = 10 *乘数; count ++)
{
for int i = 10 ; i < 50 ; i ++)
{
// 工作。
}
}
}


public int m;

for(m = 1; m<4; ; m++)
{

}



Now based on the value of m, I need the below loop to run that number of times.
I mean to say: if m=1, then below loop should run for 10 times 1.
if m=2 then below loop should run for 10 times 2(which will be 20).

for (int i = 10; i<50; ; i++)
{

}



What I have tried:

Can anyone please tell me how to do this..??

Thanks.

解决方案

for(m = 1; m<4; m++)
{
    for (int i = 0; i<m*10; i++)
    {
        // your code here
    }
}


The way you did your for loops makes me think you may not understand the syntax. If so, let me know and i'll clarify for you.

All you need to do is embed one for loop into the other. Take a look at the values of i and totalcounter once the program is done running. the total number of times i runs is 20 as described when m runs 2 times.

for (int m = 0; m < 2; m++)
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("i count: {0}",i);
        Console.WriteLine("totalcounter count: {0}", totalcounter);
        totalcounter += 1;
    }
}



Sample output

i count: 0
totalcounter count: 1
i count: 1
totalcounter count: 2
i count: 2
totalcounter count: 3
i count: 3
totalcounter count: 4
i count: 4
totalcounter count: 5
i count: 5
totalcounter count: 6
i count: 6
totalcounter count: 7
i count: 7
totalcounter count: 8
i count: 8
totalcounter count: 9
i count: 9
totalcounter count: 10
i count: 0
totalcounter count: 11
i count: 1
totalcounter count: 12
i count: 2
totalcounter count: 13
i count: 3
totalcounter count: 14
i count: 4
totalcounter count: 15
i count: 5
totalcounter count: 16
i count: 6
totalcounter count: 17
i count: 7
totalcounter count: 18
i count: 8
totalcounter count: 19
i count: 9
totalcounter count: 20


Option 1:

int m;
for (m = 1; m < 4; m++)
{
   for (int j = 1; j <= m * 10; j++)
   {
      for (int i = 10; i < 50; i++)
      {
         // Do work.
      }
   }   
}




Option 2:

void DoWork()
{
   int m;

   for (m = 1; m < 4; m++)
   {
      Process(m);
   }
}

void Process(int multiplier)
{
   for (int count = 1; count <= 10 * multiplier; count ++)
   {
      for (int i = 10; i < 50; i++)
      {
         // Do work.
      }
   }
}


这篇关于请帮我解决下面的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆