x % 2 > 0 在 java 中意味着什么?
what does x % 2 > 0 mean in java?
我目前正在在线学习 java 类,我正在学习循环(特别是 continue 和 break 语句)。给我的例子是:
int j = 0
while (true){
System.out.println();
j++;
System.out.print(j);
if (j%2 > 0) continue
System.out.print(" is divisible by 2");
if (j >= 10) break;
}
我不明白为什么它是 (j%2 > 0) 而不是 (j%2 == 0),因为如果 'j' 是 5 而你做了 5%2。你得到的数字不是1吗?或者我错过了什么?有人可以向我解释一下吗?
(对不起,我不是我的问题有点令人困惑。我以前从未使用过这个网站而且我还很年轻)
int j = 0;
while (true){
System.out.println();
j++;
System.out.print(j);
// in this case you won't print any message and you
// are sure that the next number is even (j will be incremented by "continue;").
if (j%2 > 0) continue;
System.out.print(" is divisible by 2");
if (j >= 10) break;
}
X % 2表示x除以2的余数。所以如果x/2的余数大于0,则表示x为奇数。当x%2 == 0时,则x为正数
我给你解释一下。请参阅每行旁边的注释。
int j = 0
while (true){
System.out.println();
j++; //increases the value of j on the next line by 1.
System.out.print(j); //prints 1, the first time because of above, 0 + 1.
if (j%2 > 0) continue //using modulus operator(%) we are doing 1 % 2, answer is 1
//since 1 % 2(if 1 is divisible by 2) > 0 we are
//continue statement breaks the iteration in the
//loop, so anything below this line won't be
//executed.
System.out.print(" is divisible by 2");//this line will only be executed
//if j is divisible by 2. that is
//j is divisible by 2 (j%2 == 0)
if (j >= 10) break; //when j is equal or greater than
//0 we are stopping the while loop.
}
Continue 的意思是“转到循环的顶部,跳过循环的其余代码”而不是“继续执行代码”。因此,由于 5%2 为 1,并且 1 > 0,continue 将执行,直接转到循环的顶部并跳过循环体的其余部分。
为什么他们使用 > 0 而不是 != 0?没有任何技术原因,这是一种风格差异。我个人会使用后者,因为它在我看来更清晰。但两者都有效。
我目前正在在线学习 java 类,我正在学习循环(特别是 continue 和 break 语句)。给我的例子是:
int j = 0
while (true){
System.out.println();
j++;
System.out.print(j);
if (j%2 > 0) continue
System.out.print(" is divisible by 2");
if (j >= 10) break;
}
我不明白为什么它是 (j%2 > 0) 而不是 (j%2 == 0),因为如果 'j' 是 5 而你做了 5%2。你得到的数字不是1吗?或者我错过了什么?有人可以向我解释一下吗? (对不起,我不是我的问题有点令人困惑。我以前从未使用过这个网站而且我还很年轻)
int j = 0;
while (true){
System.out.println();
j++;
System.out.print(j);
// in this case you won't print any message and you
// are sure that the next number is even (j will be incremented by "continue;").
if (j%2 > 0) continue;
System.out.print(" is divisible by 2");
if (j >= 10) break;
}
X % 2表示x除以2的余数。所以如果x/2的余数大于0,则表示x为奇数。当x%2 == 0时,则x为正数
我给你解释一下。请参阅每行旁边的注释。
int j = 0
while (true){
System.out.println();
j++; //increases the value of j on the next line by 1.
System.out.print(j); //prints 1, the first time because of above, 0 + 1.
if (j%2 > 0) continue //using modulus operator(%) we are doing 1 % 2, answer is 1
//since 1 % 2(if 1 is divisible by 2) > 0 we are
//continue statement breaks the iteration in the
//loop, so anything below this line won't be
//executed.
System.out.print(" is divisible by 2");//this line will only be executed
//if j is divisible by 2. that is
//j is divisible by 2 (j%2 == 0)
if (j >= 10) break; //when j is equal or greater than
//0 we are stopping the while loop.
}
Continue 的意思是“转到循环的顶部,跳过循环的其余代码”而不是“继续执行代码”。因此,由于 5%2 为 1,并且 1 > 0,continue 将执行,直接转到循环的顶部并跳过循环体的其余部分。
为什么他们使用 > 0 而不是 != 0?没有任何技术原因,这是一种风格差异。我个人会使用后者,因为它在我看来更清晰。但两者都有效。