《Java黑皮书基础篇第10版》 第3章【习题】

第三章

3.2习题

列出6个关系操作符

> , > = , < , < = , = = , ! = >, >=, <, <=, ==, != >,>=,<,<=,==,!=

假设 x 等于1,给出下列布尔表达式的结果:

(x > 0) true

(x < 0) false

(x != 0) true

(x >= 0)truw

(x != 1) truw

下面涉及类型转换的变换合法吗? 编写一个测试程序来验证你的结论。

boolean b = true;
int i = (int)b; 
int i = 1;
boolean b = (boolean)i;

不合法

直接量、保留字、标识符的区别

标识符:用于命名变量、方法、类

保留字/关键字:对编译器有特殊含义的字符,不能用于其他目的

直接量:一个程序中直接出现的数值,true和false都是直接量,但他们同时也是保留字,不能用于其他目的

3.3习题

编写一个if语句,在y大于等于0的时候将1賦值给X

import java.util.Scanner;

public class Main{
   
	public static void main(String[] args){
   
		Scanner input = new Scanner(System.in);
		System.out.print("input a value: ");
		int y = input.nextInt();
		if (y > 0) {
   
			int x = 1;
			System.out.print(x);
		}
	}
}

编写一个if 语句,如果score大于90则增加3%的支付

import java.util.Scanner;

public class Main{
   
	public static void main(String[] args){
   
		Scanner input = new Scanner(System.in);
		System.out.print("input a score: ");
		int score = input.nextInt();
		if (score > 90) {
   
			double payment = score * 1.03;
			System.out.print(payment);
		}
	}
}
3.4习题

编写一个if语句,如果score大于90则增加3%的支付,否则则增加1%的支付

import java.util.Scanner;

public class Main{
   
	public static void main(String[] args){
   
		Scanner input = new Scanner(System.in);
		System.out.print("input a score: ");
		int score = input.nextInt();
		if (score > 90) {
   
			double payment1 = score * 1.03;
			System.out.println(payment1);
		}
		else {
   
			double payment2 = score * 1.01;
			System.out.println(payment2);
		}
	}
}
3.5习题

假设x = 3 以及y = 2, 如果下面代码有输出的话,请给出。如果 x = 3并且y=4, 结果是什么呢? 如果x = 2并且y = 2, 结果是什么呢?

if (x > 2) {
   if (y > 2) {
   
​		 z = x + y;
 	    System.out.println("z is " + z);} 
}
else
System.out.println("x is " + x);

第一种情况,没有输出;第二种情况,z is 7;第三种情况,x is 2

假设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);

第一种情况,x is 2,第二种情况,没有输出,第三种情况,z is 6

下面代码中有什么错误?

public class Main{
   
	public static void main(String[] args){
   
		if (score >= 60.0)
			System.out.println("D");
		else if (score >= 70.0)
			System.out.println("C");
		else if (score >= 80.0)
			System.out.println("B"); 
		else if (score >= 90.0)
			System.out.println("A");
		else
			System.out.println("F");
		}
}

没有声明score变量,分数顺序应该从高到低

3.6习题

以下语句哪些是等价的? 哪些是合理缩进的?

if (i > 0) 
  if (j > 0)
		x = 0; 
	else if (k > 0) 
    y = 0;
else z = 0;

if (i > 0) {
   
  if (j > 0)
    x = 0;
  else if (k > 0)
    y = 0;
}
else
  z = 0;

if (i > 0)
  if (j > 0)
    x = 0;
	else if (k > 0)
    y = 0;
	else
    z = 0;

if (i > 0)
  if (j > 0)
    x = 0;
	else if (k > 0)
    y = 0;
else
  z = 0;

使用布尔表达式重写以下语句

if (count % 10== 0) 

​	newLine = true;

else

​	newLine = false;


//改写

boolean newLine = count % 10 == 0;

下列语句正确吗?哪个更好?

第二个更好,写的代码量更少

如果 number 值为 14、15 或者 30, 下列代码的输出是什么?

number = 14:14 is even

number = 15:15 is multiple of 5

number = 30:30 is even & 30 is multiple of 5

number = 14:14 is even

number = 15:15 is multiple of 5

number = 30:30 is even

3.7习题

以下哪些是调用Math.random()的可能输出? 323.4, 0.5, 34, 1.0, 0.0, 0.234

可能的:0.5、0.0、0.234

如何产生一个随机的整数i, 使得 0 ≤ i < 20 0 \leq i < 20 0i<20;

public class Main{
   
	public static void main(String[] args){
   
		double a = Math.random();
		int b = (int)(a * 20); 
		System.out.println(b);
		}
}

如何产生一个随机的整数i, 使得 10 ≤ i < 20 10 \leq i < 20 10i<20;

public class Main{
   
	public static void main(String[] args){
   
		double a = Math.random();
		int b = (int)(a * 10 + 10); 
		System.out.println(b);
		}
}

如何产生一个随机的整数i, 使得 0 ≤ i ≤ 50 0 \leq i \leq 50 0i50;

public class Main{
   
	public static void main(String[] args){
   
		double a = Math.random();
		int b = (int)(a * 50); 
		System.out.println(b);
		}
}

编写一个表达式, 随机返回0或者1

public class Main{
   
	public static void main(String[] args){
   
		double a = Math.random();
		if (a < 0.5) {
   
			System.out.print(0);
		}
		else {
   
			System.out.print(1);
		}
		}
}
3.9习题

下列两个语句等价吗?

if (income <= 10000)
  tax = income * 0.1;
else if (income <= 20000)
  tax = 1000 + (income - 10000) * 0.15;

if (income <= 10000)
  tax = income * 0.1;
else if (income > 10000 && income <= 20000)
  tax = 1000 + (income - 10000) * 0.15;

等价,可以通过以下代码证明

public class Main{
   
	public static void main(String[] args){
   
		double income = 15000;
		double tax;
		
		if (income <= 10000) {
   
			  tax = income * 0.1;
			  System.out.print(tax);
		}
			else if (income > 10000 && income <= 20000) {
   
			  tax = 1000 + (income - 10000) * 0.15;
			  System.out.print(tax);
			}
		}
}
3.10习题

假设x为1, 给出下列布尔表达式的结果:

(true) && (3 > 4)

!(x > 0) && (x > 0)

(x>0) ||(x<0)

(x != 0) || (x == 0)

(x >= 0) || (x < 0)

(x != 1) == !(x == 1)

false、false、true、true、true、true

编写一个布尔表达式满足: 若变量num中存储的数值在1到100之间时,表达式的值为true

public class Main{
   
	public static void main(String[] args){
   
		int num = 50;
		boolean a = num>=1 && num<=100;
		System.out.print(a);
		}
}

编写一个布尔表达式满足: 若变量 num中存储的数值在1到100之间,或值为负数时,表达式的值为 true

public class Main{
   
	public static void main(String[] args){
   
		int num = 50;
		boolean a = (num>=1 && num<=100) || num < 0;
		System.out.print(a);
		}
}

为| x - 5 | < 4.5编写一个布尔表达式, 为| x - 5 | > 4.5编写一个布尔表达式

public class Main{
   
	public static void main(String[] args){
   
		int x = 5;
		boolean a = Math.abs(x - 5) < 4.5;
		System.out.print(a);
    
		boolean b = Math.abs(x - 5) > 4.5;
		System.out.print(b);
	}
}

假设x和y都是int类型,下面哪些是合法的Java表达式 ?

x > y > 0

x = y && y

x /= y

x or y

x and y

(x != 0) || (x = 0)

第一个:需要分别写出x、y和0的关系,用与或非连接

第二个:单纯的数字不是条件,没有办法比较

第三个:合法

第四、五个:不是合法的表达式

第六个:=是赋值符号,==才是比较符号

以下两个表达式等同吗?

x % 2 == 0 && x % 3 == 0

x % 6 == 0

等价

如果 x 是45、67或者101, 表达式 x >= 50 && x <= 100的值是多少?

false, true, false

假设运行下面的程序时,从控制台输人的是2 3 6, 那么输出是什么?

public class Main{
   
	public static void main(String[] args) {
   

//可以不import,在这里用Scanner
java.util.Scanner input = new java.util.Scanner(System.in);

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));
	}
}
2 3 6
(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

编写布尔表达式,当年龄age大于13且小于18时结果为true

int age = 17;
System.out.print(age > 13 && age < 18);

编写布尔表达式,当体重weight大于50磅或者身高大于60英尺时结果为true

int weight = 17;
int height = 30;
System.out.print(weight > 50 || height > 60);

编写布尔表达式,当体重weight大于50磅并且身高大于60英尺时结果为true。

int weight = 17;
int height = 30;
System.out.print(weight > 50 && height > 60);

编写布尔表达式,当体重weight大于50磅或者身高大于60英尺,但是不是同时满足时结果为true

int weight = 50;
int height = 50;
System.out.print(weight > 50 ^ height > 60);
3.13习题

switch变量需要什么类型的数据? 如果在执行完case语句之后没有使用关键字break, 那么下一条要执行的语句是什么? 可以把switch语句转换成等价的if语句吗? 或者反过来可以将if语句转换成等价的 switch 语句吗? 使用 switch 语句的优点有哪些?

switch变量需要byte, short, int, char, string型数据

继续执行,直到有break或switch语句结束

都可以互相转换

switch语句可以处理多重条件的情况,使程序更简单易懂

执行下列switch语句之后,y是多少?并使用if-else重写代码。

x = 3;
y = 3;
switch (x + 3) {
   
case 6: y = 1;
default: y += 1;
}

y的值是2

public class Main{
   
	public static void main(String[] args) {
   
		int x = 2;
		int y = 3;
		if (x + 3 == 6) {
   
		y = 1;
		y += 1;
		System.out.print(y);
		}
		else {
   
		System.out.print(y);
		}
	}
}

执行下列if-else语句之后,x是多少? 使用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

public class Main{
   
	public static void main(String[] args) {
   
    //多个变量同时赋值,可以采用以下办法
		int x = 1, a = 3;
		
		switch (a) {
   
			case 1:
				x += 5;
			case 2:
				x += 10;
			case 3:
				x += 16;
				break;
			case 4:
				x += 34;
		}
		System.out.print(x);
	}
}

编写switch语句,如果day是0、1、2、3、4、5、6,分别显示Sunday、Monday、Tuesday、Wednesday、Thursday、Friday、Saturday

public class Main{
   
	public static void main(String[] args) {
   
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Input a number to indicate a day: ");
		int day = input.nextInt();
		
		switch (day) {
   
			case 0:
				System.out.println("Sunday");
				break;
			case 1:
				System.out.println("Monday");
				break;
			case 2:
				System.out.println("Tuesday");
				break;
			case 3:
				System.out.println("Wednesday");
				break;
			case 4:
				System.out.println("Thursday");
				break;
			case 5:
				System.out.println("Friday");
				break;
			case 6:
				System.out.println("Saturday");
				break;
		}
		input.close();
	}
}
3.14习题

假设你运行下面程序的时候从控制台输入 2 3 6, 将输出什么?

public class Main{
   
	public static void main(String[] args) {
   
		java.util.Scanner input = new java.util.Scanner(System.in); 
		double x = input.nextDouble();
		double y = input.nextDouble();
		double z = input.nextDouble();
		System.out.println((x < y && y < z) ? "sorted" : "not sorted");
		input.close();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值