作者pico2k (风月)
站内java
标题Re: [问题] SCJP 练习题
时间Mon Jan 7 17:29:01 2008
※ 引述《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
1F:推 forris:嗯...原来i值是被第二段程式给取代了.我了解了.thanks. 01/07 17:44