【问题标题】:Why is my nested for loop not displaying Hello World 2为什么我的嵌套 for 循环不显示 Hello World 2
【发布时间】:2016-01-24 18:38:41
【问题描述】:

这是我的嵌套 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++;
    }
}

这是我的控制台正在打印的内容。

This is the nested for loop :1
This is the nested for loop :2
This is the nested for loop :3
This is the nested for loop :4
This is the nested for loop :5
Hello World 1
This is the nested for loop :6
This is the nested for loop :7
This is the nested for loop :8

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

【问题讨论】:

  • 因为else if 执行时c 永远不会是7。
  • 那么,您是否在调试器中单步执行了每一行代码,并举例说明了变量值?这是要做的第一件事。
  • @OldProgrammer 现有的println() 语句足以用于此处的调试目的。

标签: java loops nested-loops


【解决方案1】:

让我将 cmets 添加到您当前的代码中。按顺序执行步骤 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)
}

基本上,在第一个 for 的开头,c 是 1,然后是 6,然后是 11……但从来不是 7,所以永远不会打印 "Hello World 2"

【讨论】:

  • 谢谢你的解释,现在完全明白了。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      你可以删除之前的 else 条件 -

       else if (c ==7){
          }
      

      只写 -

      if (c==7){}
      

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

      【讨论】:

        【解决方案5】:

        看下面的循环:

        for (int e = 0; e < 5; e++) {
                System.out.println("This is the nested for loop :" +c);
                c++;
        }
        

        对于外部循环的 10 次迭代,每次将 c 递增 5 次。 这意味着当 if 语句检查是否 c = 7 时,它将是 1、6、11、16 等。由于条件从未满足,消息没有打印出来。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-02
          • 1970-01-01
          • 2017-08-19
          • 2011-09-12
          • 1970-01-01
          • 2017-09-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多