[問題] SCJP 練習題
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
01/07 16:59, 2F
→
10/03 16:33, , 3F
10/03 16:33, 3F
討論串 (同標題文章)