第三章:选择
复习题
3.1 列出 6 个关系操作符。
解:
>,<,=,>=,<=,!=
3.2 假设 x 等于 1,给出下列布尔表达式的结果:
(x > 0)
(x < 0)
(x != 0)
(x >= 0)
(x != 1)
解:结果如下
true
false
true
true
false
3.3 下面涉及类型转换的变换合法吗?编写一个测试程序来验证你的结论。
boolean b = true;
int i = (int)b;
int i2 = 1;
boolean b2 = (boolean) i2;
解:
不合法,会报错。如下图所示:
代码问题如下:
3.4 编写一个 if 语句,在 y 大于等于 0 的时候将 1 赋值给 x。
解:代码如下
public class Review3_4 {
public static void main(String[] args) {
int x = 0;
Scanner input = new Scanner(System.in);
System.out.print("输入y的值:");
double y = input.nextDouble();
if (y >= 0) {
x = 1;
}
System.out.println("x的值为:" + x);
}
}
3.5 编写一个 if 语句,如果 score 大于 90 则增加 3% 的支付。
解:代码如下
public class Review3_5 {
public static void main(String[] args) {
int score;
Scanner input = new Scanner(System.in);
System.out.print("请输入分数:");
score = input.nextInt();
if (score >= 90) {
double a;
a = score * (1+0.03);
System.out.println(a);
} else {
double b;
b = score;
}
}
}
3.6 编写一个 if 语句,如果 score 大于 90 则增加 3% 的支付,否则则增加 1% 的支付。
解:代码如下
public class Exercise3_6 {
public static void main(String[] args) {
int score;
Scanner input = new Scanner(System.in);
System.out.print("请输入分数:");
score = input.nextInt();
if (score >= 90) {
double a;
a = score * (1+0.03);
System.out.println(a);
} else {
double b;
b = score * (1+0.01);
System.out.println(b);
}
}
}
3.7 如果 number 是 30,a 和 b 中的代码输出是多少?如果 number 是 35 呢?
a 中代码如下:
if (number % 2 == 0)
System.out.println(number + " is even.");
System.out.println(number + " is odd.");
b 中代码如下:
if (number % 2 == 0)
System.out.println(number + " is even.");
else
System.out.println(number + " is odd.");
解:代码如下
public class Exercise3_7_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入 number 的值:");
double number = input.nextDouble();
if (number % 2 == 0)
System.out.println("a - " + number + " is even.");
System.out.println("a - " + number + " is odd.");
if (number % 2 == 0)
System.out.println("b - " + number + " is even.");
else
System.out.println("b - " + number + " is odd.");
}
}
当 number 是 30,a 和 b 的输出如下:
a - 30.0 is even.
a - 30.0 is odd.
b - 30.0 is even.
当 number 是 35,a 和 b 的输出如下:
a - 35.0 is odd.
b - 35.0 is odd.
3.8 假设 x=3 以及 y=2,如果下面代码有输出的话,请给出。如果 x=3 并且 y=4,结果是什么呢?如果 x=2 并且 y=2,结果是什么呢?绘制代码的流程图。
if (x > 2) {
if (y > 2) {
int z = x + y;
System.out.println("z is " + z);
}
}
else
System.out.println("x is " + x);
解:代码如下
public class Exercise3_8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入 x 的值:");
int x = input.nextInt();
System.out.print("请输入 y 的值:");
int y = input.nextInt();
if (x > 2) {
if (y > 2) {
int z = x + y;
System.out.println("z is " + z);
}
}
else
System.out.println("x is " + x);
}
}
- 当 x=2 以及 y = 2,没有输出。
- 当 x=3 并且 y=4,输出如下
z is 7
- 当 x=2 并且 y=2,输出如下
x is 2
代码流程图如下:
3.9 假设 x=2 以及 y=3,如果下面代码有输出的话,请给出。如果 x=3 并且 y=2,结果是什么呢?如果 x=3 并且 y=3,结果是什么呢?
if (x > 2)
if (y > 2) {
int z = x + y;
System.out.println("z is " + z);
}
else
System.out.println("x is " + x);
解:代码如下
public class Exercise3_9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入 x 的值:");
int x = input.nextInt();
System.out.println("请输入 y 的值:");
int y = input.nextInt();
if (x > 2)
if (y > 2) {
int z = x + y;
System.out.println("z is " + z);
}
else
System.out.println("x is " + x);
}
}
- 当 x=2 以及 y = 3,没有输出。
- 当 x=3 并且 y=2,输出如下
x is 3
- 当 x=3 并且 y=3,输出如下
z is 6
3.10 下面代码中有什么错误?
if (score >= 60)
System.out.println("D");
else if (score >= 70)
System.out.println("C");
else if (score >= 80)
System.out.println("B");
else if (score >= 90)
System.out.println("A");
else
System.out.println("F");
解:逻辑错误,代码如下
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else if (score >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
3.11 以下语句哪些是等价的?哪些是合理缩进的?
解:a、c、d 是等价的;b、c 是合理缩进的。
3.12 使用布尔表达式重写以下语句。
if (count % 10 == 0)
newLine = true;
else
newLine = false;
解:代码如下
boolean newLine = count % 10 == 0;
3.13 下面语句正确吗?哪个更好?
解:
语句都是正确的,语句 b 更好
3.14 如果 number 值为 14、15 或者 30,下列代码的输出是什么?
解:
number 为 14 的时候,a 输出为“14 is even”;b 输出为“14 is even”。
number 为 15 的时候,a 输出为“15 is multiple of 5”;b 输出为“15 is multiple of 5”。
number 为 30 的时候,a 输出为“30 is even”、“30 is multiple of 5”;b 输出为“30 is even”。
3.15 以下哪些是调用 Math.random() 的可能输出?
323.4、0.5、34、1.0、0.0、0.234
解:
Math.random() 返回带正号的 double 值,0.0 ≤ 返回值<1.0。返回值是一个伪随机选择的数。
所以上边的可能输出为 0.5、0.0、0.234。
3.16
a. 如何产生一个随机的整数 i,使得 0 ≤ i<20 ?
b. 如何产生一个随机的整数 i,使得 10 ≤ i<20 ?
c. 如何产生一个随机的整数 i,使得 0 ≤ i ≤ 50 ?
d. 编写一个表达式,随机返回 0 或者 1。
解:代码如下
int i = (int)(Math.random()*20);
int i = (int) (10+Math.random()*10);
int i = (int)(Math.random()*51);
int i = (int)(Math.random()+0.5);
3.17 下列两个语句等价吗?
解:
等价。
3.18 假设 x 为 1,给出下列布尔表达式的结果:
解:
- false
- false
- true
- true
- true
- false
3.19 (a) 编写一个布尔表达式满足:若变量 num 中存储的数值在 1 到 100 之间时,表达式的值为 true。
(b)编写一个布尔表达式满足:若变量 num 中存储的数值在 1 到 100 之间,或值为负数时,表达式的值为 true。
解:代码如下
public class Review3_19 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入 num 的数值:");
int num = input.nextInt();
if (num >= 1 && num <= 100) {
System.out.println("true");
}
if ((num >=1 && num <= 100) || num < 0) {
System.out.println("true");
}
}
}
3.20(a)为 |x - 5|<4.5 编写一个布尔表达式。
(b)为|x - 5|>4.5 编写一个布尔表达式。
解:
A 代码如下
Math.abs(x - 5) < 4.5
B代码如下
Math.abs(x - 5) > 4.5
3.21 假设 x 和 y 都是 int 类型,下面哪些是合法的 Java 表达式?
x > y > 0
x = y = && y
x /= y
x or y
x and y
(x != 0) || (x = 0)
解:
假设 x 为 1,y 为 2。
代码如下
public class Review3_20 {
public static void main(String[] args) {
int x = 1;
int y = 2;
x > y > 0;
x = y = && y;
x /= y;
x or y;
x and y;
(x != 0) || (x = 0);
}
}
在编辑器中发现:
x > y > 0;
不合法。运算符 ‘>’ 不能应用于 ‘boolean’、‘int’
x = y = && y;
不合法。应为表达式
x /= y;
合法。
x or y;
不合法。不是语句
x and y;
不合法。不是语句
(x != 0) || (x = 0);
不合法。运算符 ‘||’ 不能应用于 ‘boolean’、‘int’
3.22 以下两个表达式等同吗?
解:
a 代码的意思是:既能被 2 整除也能被 3 整除的整数,即 2 和 3 的公倍数,而代码 b 的意思是:能够被 6 整除的整数。
因为 6 是 2 和 3 的最小公倍数,所以两个表达式等同。
3.23 如果 x 是 45、67 或者 101,表达式 x ≥ 50 && x ≤ 100 的值是多少?
解:
x = 45,值为 false
x = 67,值为 true
x = 101,值为 false
3.24 假设运行下面的程序时,从控制台输入的是 2 3 6,那么输出是什么?
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入三个数值:");
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();
System.out.println("(x < y && y < z) is " + (x < y && y < z));
System.out.println("(x < y || y < z) is " + (x < y || y < z));
System.out.println("!(x < y) is " + !(x < y));
System.out.println("(x + y < z) is " + (x + y < z));
System.out.println("(x + y > z) is " + (x + y > z));
}
}
解:输出如下
(x < y && y < z) is true
(x < y || y < z) is true
!(x < y) is false
(x + y < z) is true
(x + y > z) is false
3.25 编写布尔表达式,当年龄 age 大于 13 且小于 18 时结果为 true。
解:代码如下
if (age > 13 && age < 18) {
System.out.println("true");
}
3.26 编写布尔表达式,当体重 weight 大于 50 磅或者身高大于 60 英尺时结果为 true。
解:代码如下
if (weight > 50 || height > 60) {
System.out.println("true");
}
3.27 编写布尔表达式,当体重 weight 大于 50 磅并且身高大于 60 英尺时结果为 true。
解:代码如下
if (weight > 50 && height > 60) {
System.out.println("true");
}
3.28 3.26 编写布尔表达式,当体重 weight 大于 50 磅或者身高大于 60 英尺,但是,不是同时满足时结果为 true。
解:代码如下
if ((weight > 50) ^ (height > 60)) {
System.out.println("true");
}
3.29 switch 变量需要什么类型的数据?如果在执行完 case 语句之后没有使用关键字 break,那么下一句要执行的语句是什么?可以把 switch 语句转换成等价的 if 语句吗?或者反过来可以将 if 语句转换成等价的 switch 语句吗?使用 switch 语句的优点有哪些?
解:
switch 语句根据 char、byte、short、int 或者 String 类型的 switch 表达式来决定控制。在 switch 语句中,关键字 break 是可选的,但它通常用在每个分支的结尾,以中止执行 switch 语句的剩余部分。如果没有出现 break语句,则执行接下来的 case 语句。
3.30 执行下列 switch 语句之后,y 是多少? 并使用 if-else 重写代码。
int x = 3;
int y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}
解:switch语句的输出为“y = 2”。(因为没有使用 break 跳出,所以当 case 6 满足条件时,y = 1,没有跳出,y 继续加 1,y = 2)。
将其改为 if-else 后代码如下:
public class Exercise3_30 {
public static void main(String[] args) {
int x = 3;
int y = 3;
if ((x + 3) == 6) {
y = 1;
} else {
y += 1;
}
System.out.println("y = " + y);
}
}
3.31 执行下列 if-else 语句之后,x 是多少?使用 switch 语句重写,并且为新的 switch 语句画出流程图。
int x = 1, a = 3;
if (a == 1)
x += 5;
else if (a == 2)
x += 10;
else if (a == 3)
x += 16;
else if (a == 4)
x += 34;
解:上边语句执行后,x = 17
switch重写语句如下:
int x = 1, a = 3;
switch (a) {
case 1: x += 5; break;
case 2: x += 10; break;
case 3: x += 16; break;
case 4: x += 34; break;
}
3.32 编写 switch 语句,如果 day 是 0、1、2、3、4、5、6,分别显示 Sunday、Monday、Tuesday、Tuesday、Wednesday、Thursday、Friday、Saturday。
解:代码如下
public class Exercise3_32 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of day: ");
int day = input.nextInt();