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
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,因此第二个三元条件操作符条件永远不会成立。
