Given the code fragment:



double discount = 0;
int qty = Integer.parseInt(args[0]);
// line n1;

And given the requirements:

If the value of the qty variable is greater than or equal to 90, discount = 0.5
If the value of the qty variable is between 80 and 90, discount = 0.2

Which two code fragments can be independently placed at line n1 to meet the requirements?

A.

if (qty >= 90) {discount = 0.5; }
if (qty > 80 && qty < 90) {discount = 0.2; }

B.

discount = (qty >= 90) ? 0.5 : 0;
discount = (qty > 80) ? 0.2 : 0;

C.

discount = (qty >= 90) ? 0.5 : (qty > 80) ? 0.2 : 0;

D.

if (qty > 80 && qty < 90) {
    discount = 0.2;
} else {
    discount = 0;
}
if (qty >= 90) {
    discount = 0.5;
} else {
    discount = 0;
}

E.

discount = (qty > 80) ? 0.2 : (qty >= 90) ? 0.5 : 0;

題解

選項A,程式邏輯符合題義。

選項B,qty若大於等於90,那它一定會大於80,因此第7行程式沒有作用,會被第8行的結果覆蓋。

選項C,程式邏輯符合題義。

選項D,qry若介於80到90之間,discount會在第二個if結構中,數值被改成0。

選項E,qty若大於等於90,那它一定會大於80,因此第二個三元條件運算子條件永遠不會成立。