Given the code fragment:



System.out.println(2 + 4 * 9 - 3); //Line 21
System.out.println((2 + 4) * 9 - 3); // Line 22
System.out.println(2 + (4 * 9) - 3); // Line 23
System.out.println(2 + 4 * (9 - 3)); // Line 24
System.out.println((2 + 4 * 9) - 3); // Line 25

Which line of codes prints the highest number?

A. Line 21
B. Line 22
C. Line 23
D. Line 24
E. Line 25

題解

Java程式的算式遵循「先乘除後加減」與「括號內先計算」的四則運算規擇。

第21行的算法為:

2 + 4 * 9 - 3 = 2 + 36 - 3 = 38 - 3 = 35

第22行的算法為:

(2 + 4) * 9 - 3 = 6 * 9 - 3 = 54 - 3 = 51

第23行的算法為:

2 + (4 * 9) - 3 = 2 + 36 - 3 = 38 - 3 = 35

第24行的算法為:

2 + 4 * (9 - 3) = 2 + 4 * 6 = 2 + 24 = 26

第25行的算法為:

(2 + 4 * 9) - 3 = (2 + 36) - 3 = 38 - 3 = 35