200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第三章

Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第三章

时间:2019-06-19 05:30:36

相关推荐

Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第三章

第三章:选择

复习题

3.1 列出 6 个关系操作符。

解:

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

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

(x > 0)

(x < 0)

(x != 0)

(x >= 0)

(x != 1)

解:结果如下

truefalsetruetruefalse

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.");elseSystem.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.");elseSystem.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);}}elseSystem.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);}}elseSystem.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);}elseSystem.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);}elseSystem.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");elseSystem.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;elsenewLine = 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,给出下列布尔表达式的结果:

解:

falsefalsetruetruetruefalse

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

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

public class Exercise3_33 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter three numbers: ");double x = input.nextDouble();double y = input.nextDouble();double z = input.nextDouble();System.out.println((x < y && y < z) ? "sorted" : "not sorted");}}

解:输出为 sorted。

3.34 运用条件操作符重写以下 if 语句。

解:代码如下

ticketPrice = (ages >= 16) ? 20 : 10;

3.35 使用 if-else 表达式重写下面的条件表达式。

解:

(a) 表达式如下

if (x > 10) {score = 3 * scale;} else {score = 4 * scale;}

(b) 表达式如下

if(income > 10000) {tax = income * 0.2;} else {tax = income * 0.17 + 1000;}

© 表达式如下

if(number % 3 == 0) {System.out.println(i);} else {System.out.println(j);}

3.36 编写随机返回 1 或者 -1 的条件表达式。

解:

代码如下

public class Exercise3_36 {public static void main(String[] args) {int i = new Random().nextBoolean() ? 1 : -1;System.out.println(i);}}

3.37 列出布尔操作符的优先级顺序。计算下面的表达式:

true || true && falsetrue && true || false

解:

优先级顺序如下:

! (非) > ^ (异或) > && (逻辑与、条件与) > || (逻辑或、条件或)。

表达式结果如下:

true、true

3.38 除 = 之外的所有二次元操作符都是左结合的,这个说法是真还是假?

解:

这个说法是真的。所有二元运算符都是左结合的,也就是运算按从左到右的顺序执行。

3.39 计算下面的表达式:

2 * 2 - 3 > 2 && 4 - 2 > 52 * 2 - 3 > 2 || 4 - 2 > 5

解:

false、false

3.40 ( x > 0 && x < 10) 和 ((x >0) && (x < 10)) 是否一样?

((x > 0) || (x < 10)) 是否一样?

(x > 10 || x < 10 && y < 0) 和 (x > 0 || (x < 10 && y < 0)) 是否一样?

解:

三个都是一样的。

编程练习题

解:代码如下

public class Test_3_1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double v = Math.pow(b, 2) - 4 * a * c;double r1,r2;if (v > 0) {r1 = ((-b) + Math.pow(v , 0.5)) / (2 * a);r2 = ((-b) - Math.pow(v , 0.5)) / (2 * a);System.out.println("The equation has two roots " + r1 + " and " + r2);} else if (v == 0) {r1 = ((-b) + Math.pow(v , 0.5)) / (2 * a);System.out.println("The equation has one root " + r1);} else {System.out.println("The equation has no real roots");}}}

解:代码如下

public class Test_3_2 {public static void main(String[] args) {int number01 = (int) System.currentTimeMillis() % 10;int number02 = (int) System.currentTimeMillis() / 7 % 10;int number03 = (int) System.currentTimeMillis() / 4 % 10;Scanner input = new Scanner(System.in);System.out.println("What is " + number01 +" + "+ number02 +" + "+ number03 +" ? ");int answer = input.nextInt();System.out.println(number01 +" + "+ number02 +" + "+ number03 + " = " + answer +" is " +(number01 + number02 + number03 == answer));}}

解:代码如下

public class Test_3_3 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c, d, e, f: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double d = input.nextDouble();double e = input.nextDouble();double f = input.nextDouble();double t = a * d - b * c , x , y ;if(t != 0){x = ((e * d - b * f) / t);y = ((a * f - e * c) / t);System.out.println("x is " + x + " and " + "y is " +y);}else{System.out.println("The equation has no solution");}}}

解:代码如下

public class Test_3_4 {public static void main(String[] args) {int month = (int)(System.currentTimeMillis() % 13);switch (month){case 1 : System.out.println("The number " + month + " is " + "January"); break;case 2 : System.out.println("The number " + month + " is " + "February"); break;case 3 : System.out.println("The number " + month + " is " + "March"); break;case 4 : System.out.println("The number " + month + " is " + "April"); break;case 5 : System.out.println("The number " + month + " is " + "May"); break;case 6 : System.out.println("The number " + month + " is " + "June"); break;case 7 : System.out.println("The number " + month + " is " + "July"); break;case 8 : System.out.println("The number " + month + " is " + "August"); break;case 9 : System.out.println("The number " + month + " is " + "September"); break;case 10 : System.out.println("The number " + month + " is " + "October"); break;case 11 : System.out.println("The number " + month + " is " + "November"); break;case 12 : System.out.println("The number " + month + " is " + "December"); break;}}}

解:代码如下

public class Test_3_5 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter today's day: ");int today = input.nextInt();System.out.print("Enter the number of days elapsed since today: ");int days = input.nextInt();switch (today) {case 0 : System.out.print("Today is Sunday ");break;case 1 : System.out.print("Today is Monday ");break;case 2 : System.out.print("Today is Tuesday ");break;case 3 : System.out.print("Today is Wednesday ");break;case 4 : System.out.print("Today is Thursday ");break;case 5 : System.out.print("Today is Friday ");break;case 6 : System.out.print("Today is Saturday ");break;}days = days % 7;int future = days + today;if(future > 7){future = future - 7;}switch (future) {case 0 : System.out.println("and the future day is Sunday ");break;case 1 : System.out.println("and the future day is Monday ");break;case 2 : System.out.println("and the future day is Tuesday ");break;case 3 : System.out.println("and the future day is Wednesday ");break;case 4 : System.out.println("and the future day is Thursday ");break;case 5 : System.out.println("and the future day is Friday ");break;case 6 : System.out.println("and the future day is Saturday ");break;}}}

解:代码如下

public class Test_3_6 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter weight in pounds:");double weight = input.nextDouble();System.out.print("Enter feet:");double feet = input.nextDouble();System.out.print("Enter inches:");double inches = input.nextDouble();inches = feet * 12 + inches;final double KIL0GRAMS_PER_P0UND = 0.45359237;final double METERS_PER_INCH = 0.0254;double weightInKilograms = weight * KIL0GRAMS_PER_P0UND;double heightInMeters = inches * METERS_PER_INCH;double bmi = weightInKilograms / (heightInMeters * heightInMeters);System.out.println("BMI is " + bmi);if (bmi < 18.5)System.out.println("Underweight");else if (bmi < 25)System.out.println("Normal");else if (bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");}}

解:代码如下

public class Test_3_7 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);int numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;int numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;int numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;int numberOfNickels = remainingAmount /5;remainingAmount =remainingAmount % 5;int numberOfPennies = remainingAmount;System.out.println("Your amount " + amount + " consists of ");if(numberOfOneDollars == 1)System.out.println("" + numberOfOneDollars + " dollar");else if (numberOfOneDollars > 1)System.out.println("" + numberOfOneDollars + " dollars");if(numberOfQuarters == 1)System.out.println("" + numberOfQuarters + " quarter");else if (numberOfQuarters > 1)System.out.println("" + numberOfQuarters + " quarters");if(numberOfDimes == 1)System.out.println("" + numberOfDimes + " dime");else if (numberOfDimes > 1)System.out.println("" + numberOfDimes + " dimes");if(numberOfNickels == 1)System.out.println("" + numberOfNickels + " nickel");else if (numberOfNickels > 1)System.out.println("" + numberOfNickels + " nickels");if(numberOfPennies == 1)System.out.println("" + numberOfPennies + " penny");else if (numberOfPennies > 1)System.out.println("" + numberOfPennies + " pennies");}}

解:代码如下

public class Test_3_8 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter a,b,c: ");int a = input.nextInt();int b = input.nextInt();int c = input.nextInt();int t;if (a > b){t = a;a = b;b = t;}if (a > c){t = a;a = c;c = t;}if (b > c){t = b;b = c;c = t;}System.out.println("The result is : " + a + " " + b + " " + c);}}

解:代码如下

public class Test_3_9 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter number: ");int d1 , d2 , d3 , d4 , d5 , d6 , d7 , d8 , d9 , retain;int number = input.nextInt();retain = number;d1 = 0;d2 = number / 10000000;number = number % 10000000;d3 = number / 1000000;number = number % 1000000;d4 = number / 100000;number = number % 100000;d5 = number / 10000;number = number % 10000;d6 = number / 1000;number = number % 1000;d7 = number / 100;number = number % 100;d8 = number / 10;number = number % 10;d9 = number;int t =(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;if(t == 10)System.out.println("The ISBN-10 number is " + "0" + retain + "X");elseSystem.out.println("The ISBN-10 number is " + "0" + retain + t);}}

解:代码如下

public class Test_3_10 {public static void main(String args[]){int number1 = (int)(Math.random() * 10);int number2 = (int)(Math.random() * 10);System.out.print("What`s " + number1 + "+" + number2 + " ? ");Scanner input = new Scanner(System.in);int answer = input.nextInt();if (number1 + number2 == answer)System.out.println("You are correct!");else{System.out.println("You answer is wrong.");System.out.println(number1 + "+" + number2 + " should be " + (number1 + number2));}}}

解:代码如下

public class Test_3_11 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter month number 1~12: ");int month = input.nextInt();System.out.print("Enter year number: ");int year = input.nextInt();if (month == 2){if (year % 4 == 0 && year % 100 != 0 || year % 400 ==0){System.out.println("February " + year + " has 29 days");}else{System.out.println("February " + year + " has 28 days");}}else{switch (month){case 1 : System.out.println("January "+ year + " has 31 days");break;case 3 : System.out.println("March "+ year + " has 31 days");break;case 4 : System.out.println("April "+ year + " has 30 days");break;case 5 : System.out.println("May "+ year + " has 31 days");break;case 6 : System.out.println("June "+ year + " has 30 days");break;case 7 : System.out.println("July "+ year + " has 31 days");break;case 8 : System.out.println("August "+ year + " has 31 days");break;case 9 : System.out.println("September "+ year + " has 30 days");break;case 10 : System.out.println("October "+ year + " has 31 days");break;case 11 : System.out.println("November "+ year + " has 30 days");break;case 12 : System.out.println("December "+ year + " has 31 days");break;}}}}

解:代码如下

public class tt1 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer: ");int number = input.nextInt();int front , later;later = number % 10;front = number / 100;if (later == front)System.out.println(number + " is a palindrome");elseSystem.out.println(number + " is not a palindrome");}}

解:代码如下

public class Test_3_13 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.println("(0-single filer, 1-married jointly or " );System.out.println("qualifying widow(er), 2-married separately, 3-head of ");System.out.print("household) Enter the filing status: ");int status = input.nextInt();System.out.print("Enter the taxable income : ");double income = input.nextDouble();double tax = 0;if (status == 0){if (income <= 8350)tax = income * 0.10;else if (income <= 33950)tax = 8350 * 0.10 + (income - 8350) * 0.15;else if (income <= 82250)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;else if (income <= 171550)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;else if (income <= 372950)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;elsetax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}else if (status == 1){if (income <= 16700)tax = income * 0.10;else if (income <= 67900)tax = 16700 * 0.10 + (income - 16700) * 0.15;else if (income <= 137050)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;else if (income <= 208850)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;else if (income <= 372950)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;elsetax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;}else if (status == 2){if (income <= 8350)tax = income * 0.10;else if (income <= 33950)tax = 8350 * 0.10 + (income - 8350) * 0.15;else if (income <= 68525)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;else if (income <= 104425)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;else if (income <= 186475)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;elsetax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;}else{if (income <= 11950)tax = income * 0.10;else if (income <= 45500)tax = 11950 * 0.10 + (income - 11950) * 0.15;else if (income <= 117450)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;else if (income <= 190200)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117451 - 45500) * 0.25 + (income - 117451) * 0.28;else if (income <= 375950)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117451 - 45500) * 0.25 + (190200- 117451) * 0.28 + (income - 190200) * 0.33;elsetax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117451 - 45500) * 0.25 + (190200- 117451) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;}System.out.println("Tax is " + (int)(tax * 100) / 100.0);}}

解:代码如下

public class Test_3_14 {public static void main(String args[]){int result = (int)(Math.random() *100 % 2);Scanner input = new Scanner(System.in);System.out.print("Enter your guess: ");int guess = input.nextInt();if (guess == result)System.out.println("Your guess is right!");elseSystem.out.println("Your guess not is right!");}}

解:代码如下

public class Test_3_15 {public static void main(String args[]){int lottery = (int)(Math.random() * 1000);Scanner input = new Scanner(System.in);System.out.print("Enter your lottery (three digits): ");int guess = input.nextInt();int retain_lottery=lottery;int lotteryDigit1 = lottery / 100;lottery = lottery % 100;int lotteryDigit2 = lottery / 10;int lotteryDigit3 = lottery % 10;int guessDigit1 = guess / 100;guess = guess % 100;int guessDigit2 = guess / 10;int guessDigit3 = guess % 10;System.out.println("The lottery is " + retain_lottery);if (guess == lottery)System.out.println("Exact match: you win $10,000");else if (guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit3 && guessDigit3 ==lotteryDigit1)System.out.println("Exact match: you win $3,000");else if (guessDigit1 == lotteryDigit1 ||guessDigit2 == lotteryDigit2 || guessDigit3 == lotteryDigit3)System.out.println("Exact match: you win $1,000");elseSystem.out.println("Sorry,no match");}}

解:代码如下

public class Test_3_16 {public static void main(String args[]){int maxX = 50 , minX = -50;int maxY = 100 , minY = -100;Random random = new Random();int x = random.nextInt(maxX) % (maxX - minX +1) + minX;int y = random.nextInt(maxY) % (maxY - minY +1) + minY;System.out.println("( " + x + " , " + y + " )");}}

解:代码如下

public class Test_3_17 {public static void main(String args[]){int computer = (int)(Math.random() * 100 % 3);Scanner input = new Scanner(System.in);System.out.print("scissor (0), rock (1), paper (2): ");int you = input.nextInt();if(computer == you){if(computer == 0)System.out.println("The computer is rock. You are rock too. Tt is a draw ");if(computer == 1)System.out.println("The computer is scissor. You are scissor too. Tt is a draw ");if(computer == 3)System.out.println("The computer is paper. You are paper too. Tt is a draw ");}else if (computer == 0){if(you == 1)System.out.println("The computer is rock. You are scissor. You lost");if(you == 2)System.out.println("The computer is rock. You are paper. You win");}else if (computer == 1){if(you == 0)System.out.println("The computer is scissor. You are rock. You win");if(you == 2)System.out.println("The computer is scissor. You are paper. You lost");}else if (computer == 2){if(you == 0)System.out.println("The computer is paper. You are rock. You lost");if(you == 1)System.out.println("The computer is paper. You are scissor. You win");}}}

解:代码如下

public class Test_3_18 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter weight: ");int w = input.nextInt();if(w > 0 && w <=1)System.out.println("The dollar is $3.5");else if (w >1 && w <= 3)System.out.println("The dollar is $5.5");else if (w > 3 && w <= 10)System.out.println("The dollar is $8.5");else if (w > 10 && w <= 20)System.out.println("The dollar is $10.5");elseSystem.out.println("the package cannot be shipped");}}

解:代码如下

public class Test_3_19 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter three side: ");int a = input.nextInt();int b = input.nextInt();int c = input.nextInt();if((a * a + b * b) >(c * c) && (a * a + c * c) >(b * b) && (c * c + b * b) >(a * a))System.out.println("This is legal!");elseSystem.out.println("This is illegal");}}

解:代码如下

public class Test_3_20 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter the temperature in Fahrenheit between -58F and 41F :");double t = input.nextDouble();System.out.print("Enter the wind speed (>-2) in miles per hour:");double v = input.nextDouble();double Twc;if((t >= -58 && t <=41) && (v >= 2)){Twc = 35.74 + 0.6215 * t - 35.75 * Math.pow(v,0.16) + 0.4275 * t * Math.pow(v,0.16);Twc = (int)(Twc * 100000);Twc = Twc / 100000;System.out.println("The wind chill index is " + Twc);}elseSystem.out.println("The open temperature or wind speed is illegal");}}

解:代码如下

public class Test_3_21 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter year:(e.g., ):");int year = input.nextInt();System.out.print("Enter month: 1-12:");int m = input.nextInt();if (m == 1){m = 13;year = year -1;}if (m == 2) {m = 14;year = year -1;}System.out.print("Enter the day of the month: 1-31:");int q = input.nextInt();int j = year / 100;int k = year % 100;int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;switch (h){case 0 : System.out.println("Day of the week is Saturday");break;case 1 : System.out.println("Day of the week is Sunday");break;case 2 : System.out.println("Day of the week is Monday");break;case 3 : System.out.println("Day of the week is Tuesday");break;case 4 : System.out.println("Day of the week is Wednesday");break;case 5 : System.out.println("Day of the week is Thursday");break;case 6 : System.out.println("Day of the week is Friday");break;}}}

解:代码如下

public class Test_3_22 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter a point with two coordinates: ");double x = input.nextDouble();double y = input.nextDouble();Double x0 = 0.0 , y0 = 0.0 ;double length = Math.pow(Math.pow(x - x0 , 2) + Math.pow(y - y0 , 2), 0.5);if (length <= 10.0)System.out.println("Point (" + x + " , " + y + ") is in the circle");elseSystem.out.println("Point (" + x + " , " + y + ") is not in the circle");}}

解:代码如下

public class Test_3_23 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter a point with two coordinates: ");double x = input.nextDouble();double y = input.nextDouble();if (x <= 10.0 / 2 && x >= -(10.0 /2) && y <= 5.0 / 2 && y >= -(5.0 / 2))System.out.println("Point (" + x + " , " + y + ") is in the rectangle");elseSystem.out.println("Point (" + x + " , " + y + ") is not in the rectangle");}}

解:代码如下

public class Test_3_24 {public static void main(String args[]){int number = (int)(Math.random() * 13 + 1);int flower = (int)(Math.random() * 4 + 1);System.out.print("The card you pick is ");switch (number){case 1 : System.out.print("Ace ");break;case 2 : System.out.print("2 ");break;case 3 : System.out.print("3 ");break;case 4 : System.out.print("4 ");break;case 5 : System.out.print("5 ");break;case 6 : System.out.print("6 ");break;case 7 : System.out.print("7 ");break;case 8 : System.out.print("8 ");break;case 9 : System.out.print("9 ");break;case 10 : System.out.print("10 ");break;case 11 : System.out.print("Jack ");break;case 12 : System.out.print("Queen ");break;case 13 : System.out.print("King ");break;}switch (flower){case 1 : System.out.print(" of Clubs");break;case 2 : System.out.print(" of Diamonds");break;case 3 : System.out.print(" of Hearts");break;case 4 : System.out.print(" of Spades");break;}}}

解:代码如下

public class Test_3_25 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4:");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDouble();double y3 = input.nextDouble();double x4 = input.nextDouble();double y4 = input.nextDouble();double t = (y1-y2)*(x4-x3)-(y3-y4)*(x2-x1);if ( t != 0 ){double x = (((y1-y2)*x1-(x1-x2)*y1)*(x4-x3) - (x2-x1)*((y3-y4)*x3-(x3-x4)*y3)) / t;double y = (((y3-y4)*x3-(x3-x4)*y3)*(y1-y2) - (y3-y4)*((y1-y2)*x1-(x1-x2)*y1)) / t;System.out.println("The intersecting point is at ( " + x + " , " + y + " )");}elseSystem.out.println("The two lines are parallel");}}

解:代码如下

public class Test_3_26 {public static void main(String args[]){Scanner input = new Scanner(System.in);System.out.print("Enter an integer: ");int number = input.nextInt();System.out.print("Is 10 divisible by 5 and 6? ");if(number / 6 == 1 && number / 5 == 1)System.out.println("true");elseSystem.out.println("false");System.out.print("Is 10 divisible by 5 or 6? ");if(number / 6 == 1 || number / 5 == 1)System.out.println("true");elseSystem.out.println("false");System.out.print("Is 10 divisible by 5 or 6,but not both? ");if((number / 6 == 1 && number / 5 != 1) || (number / 6 !=1 && number / 5 ==1))System.out.println("true");elseSystem.out.println("false");}}

解:代码如下

public class Test_3_27 {public static void main(String[] args) {double xCoordinate, yCoordinate;System.out.print("Enter a point's x- and y-coordinates: ");Scanner input = new Scanner(System.in);xCoordinate = input.nextDouble();yCoordinate = input.nextDouble();double a = (200 - xCoordinate) / 200 * 100;double b = (100 - yCoordinate) / 100 * 200;// 验证坐标是否在三角形中System.out.println(xCoordinate <= b && yCoordinate <= a? "The point is in the triangle!" : "The point is not in the triangle!");}}

解:代码如下

public class Test_3_28 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter r1's center x-,y-cooordinates, width,and height: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double w1 = input.nextDouble();double h1 = input.nextDouble();System.out.print("Enter r2's center x-,y-coordinates, width,and height:");double x2 = input.nextDouble();double y2 = input.nextDouble();double w2 = input.nextDouble();double h2 = input.nextDouble();double xDistance = x1 - x2 >= 0 ? x1-x2 : x2-x1;double yDistance = y1 - y2 >= 0 ? y1-y2 : y2-y1;if (xDistance <= (w1 - w2) / 2 && yDistance <= (h1 - h2) / 2)System.out.println("r2 is inside r1");else if (xDistance <= (w1 + w2) / 2 && yDistance <= (h1 + h2) / 2)System.out.println("r2 overlaps r1");else System.out.println("r2 does not overlap r1");}}

解:代码如下

public class Test_3_29 {public static void main(String[] args) {double xCoordinateCircle1, yCoordinateCircle1, radiusCircle1;double xCoordinateCircle2, yCoordinateCircle2, radiusCircle2;double distanceFromP1ToP2;System.out.print("Enter circle1's center x-,y-coordinates,and radius: ");Scanner input = new Scanner(System.in);xCoordinateCircle1 = input.nextDouble();yCoordinateCircle1 = input.nextDouble();radiusCircle1 = input.nextDouble();System.out.print("Enter circle2's center x-,y-coordinates,and radius: ");xCoordinateCircle2 = input.nextDouble();yCoordinateCircle2 = input.nextDouble();radiusCircle2 = input.nextDouble();distanceFromP1ToP2 = Math.pow((Math.pow(xCoordinateCircle1-xCoordinateCircle2, 2)+ Math.pow(yCoordinateCircle1-yCoordinateCircle2, 2)), 0.5);if(radiusCircle1 + radiusCircle2 >= distanceFromP1ToP2) {if(radiusCircle1 - radiusCircle2 >= distanceFromP1ToP2)System.out.println("circle2 is inside circle1");elseSystem.out.println("circle2 overlaps circle1");}elseSystem.out.println("circle2 does not overlap circle1");input.close();}}

解:代码如下

public class Test_3_30 {public static void main(String[] args) {int ZoneOffset;long CurrHours, CurrMinutes, CurrSeconds;long CurrentMilliSeconds;CurrentMilliSeconds = System.currentTimeMillis();CurrSeconds = CurrentMilliSeconds / 1000 % 60;CurrMinutes = CurrentMilliSeconds / 1000 / 60 % 60;CurrHours = CurrentMilliSeconds / 1000 / 60 / 60 % 24;System.out.print("Enter the time zone offset to GMT : ");Scanner ZoneOffsetInput = new Scanner(System.in);ZoneOffset = ZoneOffsetInput.nextInt();CurrHours = (CurrHours + ZoneOffset + 24) % 24;if(CurrHours <= 12)System.out.println("The current time is "+ CurrHours + ":" + CurrMinutes + ":" + CurrSeconds + " AM");else{CurrHours %= 12;System.out.println("The current time is "+ CurrHours + ":" + CurrMinutes + ":" + CurrSeconds + " PM");}ZoneOffsetInput.close();}}

解:代码如下

public class Test_3_31 {public static void main(String[] args) {double exchangeRate, moneyAmount, exchangedAmount;final int exchangeMode;System.out.print("Enter the exchange rate from dollars to RMB: ");Scanner input = new Scanner(System.in);exchangeRate = input.nextDouble();System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");exchangeMode = input.nextInt();if(exchangeMode == 0){System.out.print("Enter the dollar amount: ");moneyAmount = input.nextDouble();exchangedAmount = moneyAmount * exchangeRate;System.out.println("$" + moneyAmount + " is " + exchangedAmount + " yuan");}else if(exchangeMode == 1){System.out.print("Enter the RMB amount: ");moneyAmount = input.nextDouble();exchangedAmount = moneyAmount / exchangeRate;System.out.println(moneyAmount + " yuan is $" + exchangedAmount);}elseSystem.out.println("Incorrect input");input.close();}}

解:代码如下

public class Test_3_32 {public static void main(String[] args) {double xP0, yP0, xP1, yP1, xP2, yP2;int discriminant;// Prompt the user to enter the three pointsScanner input = new Scanner(System.in);System.out.print("Enter three points for p0, p1, and p2: ");xP0 = input.nextDouble();yP0 = input.nextDouble();xP1 = input.nextDouble();yP1 = input.nextDouble();xP2 = input.nextDouble();yP2 = input.nextDouble();discriminant = (int)((xP1 - xP0) * (yP2 - yP0) - (xP2 - xP0) * (yP1 - yP0));if(discriminant > 0)System.out.println("p2 is on the left side of the line");else if(0 == discriminant)System.out.println("p2 is on the same line");elseSystem.out.println("p2 is on the right side of the line");input.close();}}

解:代码如下

public class Test_3_33 {public static void main(String[] args) {double weightPackage1,pricePackage1;double weightPackage2,pricePackage2;Scanner input = new Scanner(System.in);System.out.print("Enter weight and price for package 1: ");weightPackage1 = input.nextDouble();pricePackage1 = input.nextDouble();System.out.print("Enter weight and price for package 2: ");weightPackage2 = input.nextDouble();pricePackage2 = input.nextDouble();if(pricePackage1 / weightPackage1 < pricePackage2 / weightPackage2)System.out.println("Package 1 has a better price.");else if(pricePackage1 / weightPackage1 > pricePackage2 / weightPackage2)System.out.println("Package 2 has a better price.");elseSystem.out.println("Two packages have the same price.");input.close();}}

解:代码如下

public class Test_3_34 {public static void main(String[] args) {double xP0, yP0, xP1, yP1, xP2, yP2;int discriminant;Scanner input = new Scanner(System.in);// Prompt the user to enter the three pointsSystem.out.print("Enter three points for p0, p1, and p2: ");xP0 = input.nextDouble();yP0 = input.nextDouble();xP1 = input.nextDouble();yP1 = input.nextDouble();xP2 = input.nextDouble();yP2 = input.nextDouble();discriminant = (int)((xP1 - xP0) * (yP2 - yP0) - (xP2 - xP0) * (yP1 - yP0));if(0 == discriminant && xP2 <= (xP0 > xP1?xP0:xP1) && xP2 >= (xP0 > xP1?xP1:xP0))System.out.println("(" + xP2 + ", " + yP2 + ") is on the line segment from"+ " (" + xP0 + ", " + yP0 + ") to "+ "(" + xP1 + ", " + yP1 + ")" );elseSystem.out.println("(" + xP2 + ", " + yP2 + ") is not on the line segment from"+ " (" + xP0 + ", " + yP0 + ") to "+ "(" + xP1 + ", " + yP1 + ")" );input.close();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。