[英]Why does the below code increment value with 2 rather than 1?(New to Java)
[英]Why does java program terminates after executing increment operators? In the below code it only prints 2 and not the condition statements
public static void main(String[] args) {
// TODO Auto-generated method stub
int j=1;
int i = 3;
System.out.println(++j);
if((j==1) && (i==3)) {
System.out.println("j is 4 and i is 7");
}
if((j==1) || (i==4)) {
System.out.println("j is 4 or i is 4");
display();
}
为什么java程序在执行增量运算符后终止? 在下面的代码中,它只打印 2 而不是条件语句。
您的条件始终为假,因此不会执行。 您正在打印 ++j,这使得 j 的值 = 2。
所以程序打印 2 然后它检查条件 :- If((2==3)&&(3==3))
该条件返回 false,因此它会检查下一个 if 条件。 If ((2==1) || (3==4)) 此条件也返回 false,因此两个 if 语句都不会执行。
您需要更改 j 的值或您的 if 条件,以便在 if 语句中打印代码块。
因此,只打印 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.