看板 java 關於我們 聯絡資訊
※ 引述《forris (喬巴)》之銘言: : What will happen if you attempt to compile and run the following code : public class TimDig{ : public static void main(String argv[]){ : TimDig td = new TimDig(); : td.samcov(); : } : public void samcov(){ : int i=1; : int j=2; : if((i==20) && (j==(i=i*2))){ : } //在做&&時,因為 i==20 是false,所以i=i*2 不會執行 : System.out.print(i); //i=1,所以顯示1 : if((i==20) & (j==(i=i*2))){} //i=i* 2 會執行,所以i= 1*2 : System.out.print(i); //i=2,所以顯示2 : int x = i & 2; // 2 & 2 = 2,所以x=2 : System.out.print(x); //x=2,所以顯示2 : } : } : the answer is Compilation and output of 122 : 我想問 print(x) 的結果,怎麼不是 1 (i=1 成立)或是 0 (作 AND 運算)? : 剛剛我又發現一個很有趣的事:就是 : if((i==20) & (j==(i=i*2))){} : System.out.print(i); : : 這段程式中,把 {} 拿掉,或是 } 放到 System.out.print(i); 之後, : 原本 i 值就不見了。這是怎麼一回事? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.67.10.34
forris:嗯...原來i值是被第二段程式給取代了.我了解了.thanks. 01/07 17:44