简体   繁体   English

无法弄清楚为什么会打印出 63

[英]Can't figure out why this prints out 63

came across this code and tried to solve it ... but do not quite understand why it prints out 63?遇到这段代码并试图解决它......但不太明白为什么它会打印出63?

public static void main(String[]args)
{
    int x = 0;
    int y = 0;
    for(int z= 0; z < 5; z++)
    {
        if ((++x > 2)&&(++y > 2))
        {
            x++;  
        }
    }
    System.out.println(x+""+y);
}

First of all, ++x means x is being added 1, and afterwards read for the check in the if statement.首先,++x 表示 x 被加 1,然后读取 if 语句中的检查。 So x is 1 the first time you check it.所以第一次检查时 x 是 1。

But there is another thing in Java, if you check two statements, like you do in the following:但是在 Java 中还有另一件事,如果你检查两个语句,就像你在下面做的那样:

if ((++x > 2)&&(++y > 2))

If the first check fails, the second doesn not get executed.如果第一个检查失败,第二个就不会被执行。

I written the output in console, having ----- for all for loop cycles:我在控制台中写了输出,对于所有的循环周期都有 -----:

x: 0
y: 0
z: 0
if: (false && (not executed) )
x: 1
-----
x: 1
y: 0
z: 1
if: (false && (not executed) )
x: 2
-----
x: 2
y: 0
z: 2
if: (true && false)
x: 3
y: 1
-----
x: 3
y: 1
z: 3
if: (true && false)
x: 4
y: 2
-----
x: 4
y: 2
z: 4
if: (true && true)
x: 5
y: 3
x: 6
-----
63

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

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