0
據我所知,如果我有一個變量k = 5,並且我做了++ k,新值k = 6。如果我做k ++,直到k出現在該程序的第二次,這是當它被改變爲6。例如:Post/Pre-Increment混淆
k = 5;
System.out.println(k++); //prints 5, but now that k has appeared the second time, its value is incremented to 6
System.out.println(k); //prints 6
然而,在這種代碼:
class test {
public static void main(String args[]){
for(int i =0; i<10; i++){
System.out.println(i);
}
int x = 0;
x++;
System.out.println(x);
}
}
輸出:
0
1
2
3
4
5
6
7
8
9
1
在循環中,雖然變量i第二次出現(在System.out.println(i)中),但其值保持爲0.但是對於x,當它第二次出現時(在System.out.println( X); )它的值遞增。 爲什麼?我很困惑如何發佈和預增工作。
for循環在執行i ++之前運行 – softwarenewbie7331
哦,是的。 「後增量(i ++)和前增量(++ i)運算符如何在Java中工作?」已經有了我的問題的答案。抱歉發佈。主持人請刪除這個問題嗎? – User2956
對不起,我標記爲重複。你的問題實際上是關於什麼時候增量發生在for循環中。 –