【问题标题】:How do nested loop communicate with each other? [closed]嵌套循环如何相互通信? [关闭]
【发布时间】:2021-05-25 19:37:55
【问题描述】:

在进入第二个 for 循环 (for(int j=0;j

public class MyClass {
        public static void main(String args[]) {
           int count=1;
           for(int i=1;i<=5;i++){
              while(count%2!=0){
                 for(int j=0;j<=5;j++){
                    i=i+1; 
                    j=j+1;
                    count++;
                    System.out.println(+count);
                 }
              }
           }
        }
}

【问题讨论】:

  • 我不太清楚“计数是否遵循 while 循环中规定的条件”应该是什么意思。内部循环将一直运行到完成,然后返回到外部循环,外部循环可能会决定再次运行,触发内部循环的另一次运行,或者继续......
  • 多线程在哪里?

标签: java for-loop while-loop nested


【解决方案1】:

使用适当的缩进总是有助于更好地理解代码(如果您在 Eclipse 中,这可以通过按住 ctrl 和 shift 键,然后按 F 来完成)。

第二个 for 循环将继续运行并计数 'count' 直到 'j'

【讨论】:

    【解决方案2】:

    它将首先执行完整的(第二个)for 循环,并在此 for 循环完成后检查 while 循环的条件。 for循环结束时count为4,不满足while的条件,不会再次执行for循环

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2013-04-18
      相关资源
      最近更新 更多