Topic: 一道简单题,分析一下

  Print this page

1.一道简单题,分析一下 Copy to clipboard
Posted by: lzx17
Posted on: 2006-05-28 11:24

What will be the result of executing the following code?

1. boolean a = true;

2. boolean b = false;

3. boolean c = true;

4. if (a == true)

5. if (b == true)

6. if (c == true) System.out.println("Some things are true in this world");

7. else System.out.println("Nothing is true in this world!");

8. else if (a && (b = c)) System.out.println("It's too confusing to tell what is true and what is false");

9. else System.out.println("Hey this won't compile");

Choices:

a. The code won't compile

b. "Some things are true in this world" will be printed

c. "Hey this won't compile" will be printed

d. None of these

应该选 c吧,答案是d

2.Re:一道简单题,分析一下 [Re: lzx17] Copy to clipboard
Posted by: hust_need4speed
Posted on: 2006-05-28 18:38

这题你自己试试不就知道了,答案是D。
题目的代码没有使用缩进(indent),所以看起来麻烦点。
这题考的是不带 {} 的 if 的覆盖范围。

3.Re:一道简单题,分析一下 [Re: lzx17] Copy to clipboard
Posted by: lyn0948
Posted on: 2006-05-30 09:58

大哥,自己编辑一下,运行一下不就知道了吗?
我感觉是没有答案。
("It's too confusing to tell what is true and what is false");
才是正确的答案,在考SCJP的过程中,有好多答案都是错误的
所以,你最好自己到计算机上运行一下
这样才放心
哈哈

4.Re:一道简单题,分析一下 [Re: lzx17] Copy to clipboard
Posted by: gutxgc
Posted on: 2006-06-01 17:04

自己分析一下给人家看一下吗
不然给你答案又怎么样啊
你只想知道答案你自己编辑一下就OK了
不用来这里问了

5.Re:一道简单题,分析一下 [Re: lzx17] Copy to clipboard
Posted by: hzx002
Posted on: 2006-06-02 16:05

boolean a = true;
boolean b = false;
boolean c = true;
    
if (a == true){ // 1
  if (b == true){ // 2
    if (c == true) { // 3
      System.out.println("Some things are "
        + "true in this world");
    } // 3 if end here
        
    else { //此 else 与 3 匹配
      System.out.println("Nothing is true "
        + "in this world!");
    }
  } // 2 if end here
        
  else if (a && (b = c)){ // 此 else if 与 2 匹 配
    System.out.println("It's too confusing to "
      +"tell what is true and what is false");
  }
      
  else { // 与 2 匹配的 else
    System.out.println("Hey this won't compile");
  }
} // 1 end here

我已经改了一下,但没有改变程序的执行顺序
你自己看吧。注意:
//2 是
if()...
else if()...
else.....
结构; 还有 (a && (b = c))
的值为真,所以是:It's too confusing to tell what is true and what is false


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923