[問題] SCJP 練習題

看板java作者 (喬巴)時間18年前 (2008/01/07 16:47), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/2 (看更多)
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)

01/07 16:56, , 1F
& 也可以是位元運算
01/07 16:56, 1F

01/07 16:59, , 2F
嗯,那如果是位元運算,就作 AND 運算...1 AND 2 不是 0 嗎?
01/07 16:59, 2F

10/03 16:33, , 3F
因為第二小題 1 & 0 = 0 if 裡面不執行, 所以不會印
10/03 16:33, 3F
文章代碼(AID): #17WUSdRu (java)
討論串 (同標題文章)
文章代碼(AID): #17WUSdRu (java)