执行println语句的次数

时间:2015-06-29 15:08:53

标签: java loops

我有点没有得到嵌套循环,有人可以向我解释为什么以下代码被执行45次了吗?难道i +和j + +不能增加1吗?所以第一个循环应该是(0 * 0),(1 * 1),(2 * 2)等等。

for(int i = 0; i < 10; i++)
    for(int j = 0; j < i; j++)
        System.out.println(i * j);

6 个答案:

答案 0 :(得分:2)

外部循环根据i

的值运行i

第一个i值为0,最后i值为9,因为i<10

所以内循环9*10/245

循环时间= 0+1+2+.....+8+9 = 9*10/2 = 45

注意。我使用这个等式来计算循环次数之和

   1 + 2 + 3 + ... + N = N(N+1)/2

答案 1 :(得分:1)

这不是嵌套循环的工作原理,就像这样 -

  1. 首先我们进入外循环,然后我们执行内循环直到内循环退出(即j的值变为等于i),然后我们再次递增i。
  2. 例如,如果i为5,则会进入外部循环,然后j将从0开始直到4(因为我是5) )并且计算的值将是(5*0) , (5*1) , (5*2) , (5*3) , (5*4),并且在退出内循环后,它将再次i增加1i将变为6并且然后这将重复(即j将从0开始到5)。

答案 2 :(得分:1)

不打印表达式i * j,而是打印出ij的值,这样您就可以浏览并查看循环中发生的事情。 i * j的乘积对于理解它循环的次数毫无意义。

for(int i = 0; i < 10; i++)
    for(int j = 0; j < i; j++)
        System.out.println("i " + i + "j " + j ); 

答案 3 :(得分:1)

我觉得你在理解变量范围时遇到了问题。我正在重新编写代码以便更好地理解:

for(int i = 0; i < 10; i++){
        for(int j = 0; j < i; j++){
            System.out.println(i * j);
        } //end of scope of variable j
}// end of scope of variable i

你很可能会误以为在第二个循环结束时i存储值1,变量j应该存储值1.但事实并非如此。变量j被重新初始化为值0.因此,当i在第3个循环中获得值2时,内部循环执行2次,j的值为0并且1.依此类推等等。

答案 4 :(得分:1)

由于其他答案已经解释了循环,我不会这样做。但是如果你让你的代码输出更多的信息,你可以更容易地看到会发生什么。

public static void main( final String[] args )    
    {
        int counter = 0;
        for ( int i = 0; i < 10; i++ )
        {
            System.out.println( "outerloop: i = " + i );
            System.out.println( "___________________________" );
            for ( int j = 0; j < i; j++ )
            {
                System.out.println( "innerloop: i = " + i + " & j = " + j );
                System.out.println( "Counter: " + counter + " calculation: " + i + " * " + j + " = " + ( i * j ) );
                counter++;
            }
            System.out.println( "___________________________" );
        }
    }

这就是我在学校里做的事情,以了解代码的作用。

答案 5 :(得分:1)

int iCount=1;
        System.out.println("Outer for loop will begin and run for 10 times");
        for (int i = 0; i < 10; i++) {
            System.out.println("    Inner for loop will begin and run for " + i + " time(s)");
            for (int j = 0; j < i; j++) {
                System.out.println("        (" + iCount + ") Inner for loop running....." + (i * j));
                iCount++;
            }
        }
        System.out.println("Outer for loop ends");

输出: -

Outer for loop will begin and run for 10 times
    Inner for loop will begin and run for 0 time(s)
    Inner for loop will begin and run for 1 time(s)
        (1) Inner for loop running.....0
    Inner for loop will begin and run for 2 time(s)
        (2) Inner for loop running.....0
        (3) Inner for loop running.....2
    Inner for loop will begin and run for 3 time(s)
        (4) Inner for loop running.....0
        (5) Inner for loop running.....3
        (6) Inner for loop running.....6
    Inner for loop will begin and run for 4 time(s)
        (7) Inner for loop running.....0
        (8) Inner for loop running.....4
        (9) Inner for loop running.....8
        (10) Inner for loop running.....12
    Inner for loop will begin and run for 5 time(s)
        (11) Inner for loop running.....0
        (12) Inner for loop running.....5
        (13) Inner for loop running.....10
        (14) Inner for loop running.....15
        (15) Inner for loop running.....20
    Inner for loop will begin and run for 6 time(s)
        (16) Inner for loop running.....0
        (17) Inner for loop running.....6
        (18) Inner for loop running.....12
        (19) Inner for loop running.....18
        (20) Inner for loop running.....24
        (21) Inner for loop running.....30
    Inner for loop will begin and run for 7 time(s)
        (22) Inner for loop running.....0
        (23) Inner for loop running.....7
        (24) Inner for loop running.....14
        (25) Inner for loop running.....21
        (26) Inner for loop running.....28
        (27) Inner for loop running.....35
        (28) Inner for loop running.....42
    Inner for loop will begin and run for 8 time(s)
        (29) Inner for loop running.....0
        (30) Inner for loop running.....8
        (31) Inner for loop running.....16
        (32) Inner for loop running.....24
        (33) Inner for loop running.....32
        (34) Inner for loop running.....40
        (35) Inner for loop running.....48
        (36) Inner for loop running.....56
    Inner for loop will begin and run for 9 time(s)
        (37) Inner for loop running.....0
        (38) Inner for loop running.....9
        (39) Inner for loop running.....18
        (40) Inner for loop running.....27
        (41) Inner for loop running.....36
        (42) Inner for loop running.....45
        (43) Inner for loop running.....54
        (44) Inner for loop running.....63
        (45) Inner for loop running.....72
Outer for loop ends