【发布时间】:2019-12-17 06:23:23
【问题描述】:
为什么输出如下代码:13 15 17
我认为应该是:15 17 19
代码如下:
package com.example.barker;
class dog {
}
public class Bark {
public static void main(String[] args) {
Bark o = new Bark();
o.go();
}
void go(){
int y =7;
for(int x = 1; x<8; x++) {
y++;
if(x>4) {
System.out.print(++y + " ");
}
}
}
}
【问题讨论】:
-
x=1,y=8 --> x=4,y=11 然后 x = 5, y =13; x = 6,y = 15; x = 6, y =17
-
您的
y在进入if condition之前增加4 次,然后在使用++打印之前增加一次。一切看起来都不错。