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,qty 若介於 80 到 90 之間,discount 會在第二個 if 結構中,數值被改成 0。

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