Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的嵌套for循环不显示Hello World 2_Java_Loops_Nested Loops - Fatal编程技术网

Java 为什么我的嵌套for循环不显示Hello World 2

Java 为什么我的嵌套for循环不显示Hello World 2,java,loops,nested-loops,Java,Loops,Nested Loops,这是我的嵌套for循环 int c = 1; for (int d = 0; d < 10; d++) { if (c == 6) { System.out.println("Hello World 1"); } else if (c == 7) { System.out.println("Hello World 2"); } for (int e = 0; e < 5; e++) { System.ou

这是我的嵌套for循环

int c = 1;
for (int d = 0; d < 10; d++) {
    if (c == 6) {
        System.out.println("Hello World 1");
    } else if (c == 7) {
        System.out.println("Hello World 2");
    }
    for (int e = 0; e < 5; e++) {
        System.out.println("This is the nested for loop :" +c);
        c++;
    }
}

它继续打印,直到达到50,并且不显示Hello World 2。

当涉及整数“e”的嵌套循环第一次运行时,c从1到6被证明有罪退出此循环时,c'为6,因此“Hello world 1”将打印到控制台。然后,再次调用涉及“e”的循环,在循环退出之前将“c”再加上5次直到11次,这意味着下一次第一个循环通过时“c”=11,因为每次运行内部循环都会加上5次。

您的输出已经回答了您的问题。请记住,Java完全按照您键入代码的顺序执行代码。让我们考虑一下代码的执行顺序:

  • c
    从1开始
  • d
    从0开始,跳过
    if
    语句,因为没有一个条件为真
  • 嵌套for循环的增量为
    c
    6
  • d
    递增为1,并且
    if
    语句打印出
    Hello World 1
  • 嵌套的for循环将
    c
    增加到11
  • d
    增加到2,跳过
    if
    语句,因为
    c
    既不是6也不是7
  • 继续执行,直到
    d
    达到10

  • 请看以下循环:

    for (int e = 0; e < 5; e++) {
            System.out.println("This is the nested for loop :" +c);
            c++;
    }
    
    for(int e=0;e<5;e++){
    System.out.println(“这是嵌套的for循环:”+c);
    C++;
    }
    
    在外循环的10次迭代中,每次递增c 5次。
    这意味着,当if语句检查c=7时,它将是1、6、11、16等。由于该条件从未满足,因此不会打印消息。

    让我为当前代码添加注释。通过按顺序执行步骤1至7来阅读此代码

    int c = 1;
    for (int d = 0; d < 10; d++) {
        // 1. When we are here for the first time, c = 1, both if are false
        // 4. Now, c is equal to 6 so the first if is true, "Hello World 1" is printed
        // 7. Now, c is equal to 11, both if are false, nothing is printed...
        if (c == 6) {
            System.out.println("Hello World 1");
        } else if (c == 7) {
            System.out.println("Hello World 2");
        }
        for (int e = 0; e < 5; e++) {
            // 2. We reach here, c is incremented 5 times.
            // 5. c is incremented again 5 times
            System.out.println("This is the nested for loop :" +c);
            c++;
        }
        // 3. When we're here, c that was 1 is now 6 (incremented 5 times in step 2)
        // 6. When we're here for the second time, c is now 11 (6 incremented 5 times in step 5)
    }
    
    intc=1;
    对于(int d=0;d<10;d++){
    //1.当我们第一次来到这里时,c=1,如果都为假
    //4.现在,c等于6,所以第一个如果为真,“Hello World 1”被打印出来
    //现在,c等于11,如果两者都为假,则不打印任何内容。。。
    如果(c==6){
    System.out.println(“Hello World 1”);
    }else如果(c==7){
    System.out.println(“Hello World 2”);
    }
    对于(int e=0;e<5;e++){
    //2.我们到达这里,c增加5倍。
    //5.c再次递增5倍
    System.out.println(“这是嵌套的for循环:”+c);
    C++;
    }
    //3.当我们在这里时,原来是1的c现在是6(在步骤2中增加了5倍)
    //6.当我们第二次来到这里时,c现在是11(在步骤5中,6增加了5倍)
    }
    

    基本上,在的第一个
    开始时,
    c
    是1,然后是6,然后是11。。。但是从不使用7,因此不会打印“Hello World 2”。

    您可以在-

     else if (c ==7){
        }
    
    并且只写——

    if (c==7){}
    

    因为在你的代码中,当
    c
    达到6时,它跳过测试
    c==7

    ,因为
    c
    在执行
    else if
    时永远不会是7。那么,你有没有在调试器中逐行检查每一行代码,并举例说明变量值?这是要做的第一件事。@OldProgrammer现有的
    println()
    语句足以用于调试目的。感谢您的解释,现在请完全理解它。
    if (c==7){}