Susan is five years older than Matt. Three years from now Susan’s age will be twice Matt’s age. What should be in place of the following condition to solve this problem?
for (int s = 1; s <=100; s++) {
for (int m = 1; m <= 100; m++) {
if (condition)
System.out.println("Susan is " + s + " and Matt is " + m);
}
}
(s == m - 5) && (s - 3 == 2 * (m - 3))
(s == (m + 5)) && ((s + 3) == (2 * m + 3))
s == m + 5 && s + 3 == 2 * m + 6
None of the above is correct.
三年后Matt的年龄是m + 3,其两倍是2 * m + 6。算数运算优先级高于比较运算,比较运算优先级高于逻辑运算,因而括号不是必须的。
4
Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?
(x > 15 && x < 18) || (x > 10 || y < 20)
(x > 15 && x < 18) && (x > 10)
(y < 20) || (x > 15 && x < 18)
((x > 10) || (x > 15 && x < 18)) || (y < 20)
(x < 10 && y > 20) && (x < 15 || x > 18)
逻辑或连接的条件彼此可以互换顺序,因为只要其中一个成立,整个表达式就成立。
3
What would the following print?
int x = 3;
int y = 2;
if (x > 2) x++;
if (y > 1) y++;
if (x > 2) System.out.print("first ");
if (y < 3) System.out.print("second ");
System.out.print("third");
first
first second
first second third
first third
third
执行到最后一个if时y值为3,因而条件不成立。
4
What would the following print?
int x = 3;
int y = 2;
if (y / x > 0)
System.out.print("first ");
System.out.print("second ");
0 条评论