close
一.題目
Given the code fragment:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)不會印東西
文章標籤
全站熱搜