简体   繁体   English

关于循环和中断命令的问题

[英]question about loop and break command

i have tested following code which might print 0 and 2 but it print 1 and 1 why? 我已经测试了以下可能打印0和2的代码,但是为什么打印1和1?

public class break_command {


    public static void main(String[] args) {
       for (int i=0;i<10;i++){
            for (int j=1;j<10;i++){
                if ((i+j) %2==0){
                    System.out.println("i  "+ i +"  j   " +j);
                    break;
                }


            }
            break;
       }
    }

}

result//
i  1  j   1
BUILD SUCCESSFUL (total time: 0 seconds)

Is the line: 是行:

for (int j=1;j<10;i++){

Supposed to be j++ not i++? 应该是j ++而不是i ++吗?

Otherwise it means on the first iteration: 否则,它意味着在第一次迭代中:

if ((i+j) %2==0){

Will be true. 会是真的。

You are iterating i inside your second loop, not j. 您在第二个循环中迭代i,而不是j。 So initially, in the first loop, i has the value 0. Then as you enter the second loop, it is increased to 1. At this point i has the value 1 and j has the value one; 因此,最初,在第一个循环中,i的值为0。然后,当您进入第二个循环时,i的值增加为1。 their sum is even, so it prints them out and breaks out of the inner loop, breaks out of the outer loop, and you are done. 它们的和是偶数,因此将它们打印出来并跳出内部循环,跳出外部循环,您就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM