需要帮助理解"Head First Java"中的逻辑



我最近第一次开始用Java编程(只是一种爱好(,现在我正在写一本书"Head First Java";这很好,但我真的很难理解这些练习。

例如:

class Output {

void go() {

int y = 7;
for(int x = 1; x < 8; x++) {
y++;                                 // is y now 8?
if(x >4) {
System.out.println(++y + " ");  // does this make y = 9?
}
if(y > 14) {
System.out.println(" x = " + x);
break;                       // how does the break key word affect the rest of the loop?
}
}
}

public static void main(String[] args) {

Output o = new Output();

o.go();
}
}

有人能向我解释一下这个代码中发生了什么吗?

变量y必须是15,因为您在for循环中多次增加了它的值。

++y使其值增加1。CCD_ 4和CCD_。两者都会增加数字,但++i会在计算当前表达式之前增加数字,而i++会在计算表达式之后增加数字。

CCD_ 8只是从循环中存在。

最新更新