看板 java 關於我們 聯絡資訊
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))){ } System.out.print(i); if((i==20) & (j==(i=i*2))){} System.out.print(i); int x = i & 2; System.out.print(x); } } 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: 140.127.81.30 ※ 編輯: forris 來自: 140.127.81.30 (01/07 16:59)
PsMonkey:& 也可以是位元運算 01/07 16:56
forris:嗯,那如果是位元運算,就作 AND 運算...1 AND 2 不是 0 嗎? 01/07 16:59
dangerousman:因為第二小題 1 & 0 = 0 if 裡面不執行, 所以不會印 10/03 16:33