close

一.題目

Given the code fragment:

public class OCA_5 {
public static void main(String[] args) {
int x = 5;
while (isAvailable(x)) {
System.out.print(x);
}
}
public static boolean isAvailable(int x) {
return x-- > 0 ? true:false;
}
}

 

 

Which modification enables the code to print 54321?

A. Replace line 6 with System.out. print (--x) ;

B. At line 7, insert x --;

C. Replace line 6 with --x; and, at line 7, insert System.out.print (x);

D. Replace line 12 with return (x > 0) ? false: true;

 

二.答案

B

 

三.解析

 

--x是這段敘述開始就減

x--是這段敘述結束後再減。

 

isAvailable( )方法的 x--不會影響到 main方法裡面 x變數的值

 

(A)43210,印出來前,都會先減 1,所以是從4開始到0

(B)54321,先印出5,然後印到1

(C)43210,印出來前,都會先減 1,所以是從4開始到0

(D)不會印東西

arrow
arrow
    創作者介紹
    創作者 書童 的頭像
    書童

    小書童

    書童 發表在 痞客邦 留言(0) 人氣()