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

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

if (ages >= 16) 
​	ticketPrice = 20;
else
​	ticketPrice = 10;
int ages = 16;
int z = ages >= 16 ? 20 : 10;
System.out.print(z);

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

score = (x>10) ? 3 * scale : 4 * scale;

int x = 100;
int scale = 3;
if (x > 10){
  System.out.print(3 * scale):
}
else{
	System.out.print(4 * scale):
}

tax = (income > 10000) ? income * 0.2 : income * 0.17 + 1000;

int income = 50000;
if (income > 10000){
  System.out.print(0.2 * income);
}
else{
	System.out.print(0.17 * income + 1000);
}

System.out.println((number % 3 == 0) ? i : j);

int i = 5, j = 10, number = 9;
if (number % 3 == 0){
  System.out.print(i);
}
else{
  System.out.print(j);
}

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

System.out.print(Math.random() >= 0.5 ? 1 : -1);
3.15习题

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

true || true && false

true

true && true || false

true

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

计算下面的表达式:

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

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

都是false

记住与、或、异或3个操作符是逐级向下且最低的

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

全都一样

编程练习题

(代数:解一元二次方程)可以使用下面的公式求一元二次方程的两个根

b 2 − 4 a c b^2 - 4ac b24ac称作一元二次方程的判别式。如果它是正值,那么一元二次方程就有两个实数根。如果它为 0, 方程式就只有一个根。如果它是负值,方程式无实根。

编写程序,提示用户输入a、b和c的值,并且显示基于判别式的结果

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Input a, b, and c: ");
		double a = input.nextDouble();
		double b = input.nextDouble();
		double c = input.nextDouble();
		double root1 = (-b + Math.pow(b * b - 4 * a * c, 0.5)) / (2 * a);
		double root2 = (-b - Math.pow(b * b - 4 * a * c, 0.5)) / (2 * a);
		if (b * b - 4 * a * c > 0) {
			System.out.print("2 roots existed: " + root1 + " and " + root2);
		}
		else if (b * b - 4 * a * c == 0) {
			System.out.print("1 root existed: " + root1);
		}
		else {
			System.out.print("no real root existed");
		}
	}
}

(游戏:三个数的加法)程序清单3-1中的程序产生两个整数,并提示用户输人这两个整数的和。修改该程序使之能产生三个一位整数,然后提示用户输入这三个整数的和。

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int number1 = (int)(Math.random() * 10);
		int number2 = (int)(Math.random() * 10);
		int number3 = (int)(Math.random() * 10);
		int correct = number1 + number2 + number3;
		System.out.print("What is the sum of " + number1 + ", " + number2 + ", and " + number3 + ": ");
		int answer = input.nextInt();
		
		if (correct == answer) {
			System.out.println("Your answer is correct, " + number1 + " + " + number2 + " + " + number3 + " = " + correct);
		}
		else
			System.out.println("Your answer is NOT correct, " + number1 + " + " + number2 + " + " + number3 + " = " + correct);
	}
}

(代数:求解2x2线性方程)可以使用编程练习题1.13中给出的Cramer规则解线性方程组

编写程序,提示用户输入a, b, c, d, e, f, 然后显示结果。如果ad-bc为0, 报告消息 “The equation has no solution”(方程式无解)

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Input a, b, c, d, e, f accordingly: ");
		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 x = (e * d - b * f) / (a * d - b * c);
		double y = (a * f - e * c) / (a * d - b * c);
		if (a * d - b * c == 0)
			System.out.println("The equation has no solution");
		else
			System.out.println("x is " + x + ", and y is " + y);
	}
}
Input a, b, c, d, e, f accordingly: 1 2 2 4 4 5
The equation has no solution

(随机月份) 编写一个随机产生1和12之间整数的程序,并且根据数字1,2,3等,显示相应的月份: January,February,…,December

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

(找到将来的日期) 编写一个程序,提示用户输入代表今天日期的数字(周日为0, 周一为1,…, 周六为6)。同时,提示用户输入一个今天之后的天数,作为代表将来某天的数字,然后显示这天是星期几

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Input one number for weekdays or weekends: ");
		int day = input.nextInt();
		System.out.print("Input how many days you want to calculate: ");
		int number = input.nextInt();
		int result = day + number % 7;
		
		switch (day) {
		case 1:
			System.out.print("Today is Monday and ");
			break;
		case 2:
			System.out.print("Today is Tuesday and ");
			break;
		case 3:
			System.out.print("Today is Wednesday and ");
			break;
		case 4:
			System.out.print("Today is Thursday and ");
			break;
		case 5:
			System.out.print("Today is Friday and ");
			break;
		case 6:
			System.out.print("Today is Saturday and ");
			break;
		default:
			System.out.print("Today is Sunday and ");
			break;
		}
		
		switch (result) {
		case 1:
			System.out.println("the furture day is Monday");
			break;
		case 2:
			System.out.println("the furture day is Tuesday");
			break;
		case 3:
			System.out.println("the furture day is Wednesday");
			break;
		case 4:
			System.out.println("the furture day is Thursday");
			break;
		case 5:
			System.out.println("the furture day is Friday");
			break;
		case 6:
			System.out.println("the furture day is Saturday");
			break;
		default:
			System.out.println("Sunday");
			break;
		}
	}
}

(医疗应用程序:BMI)修改程序清单3-4,让用户输人重量、英尺和英寸。例如:一个人身高是5英尺10英寸,输入的英尺值就是5、英寸值为10

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter pounds: ");
		double pounds = input.nextDouble();
		System.out.print("Enter feets: ");
		double feets = input.nextDouble();
		System.out.print("Enter inches: ");
		double inches = input.nextDouble();
		
		double kilograms = pounds * 0.45359237;
        double meters = feets * 0.3048 + inches * 0.0254;
        double bmi = kilograms / (Math.pow(meters,2));
        System.out.println("BMI is " + bmi);
        if (bmi > 30) 
        	System.out.println("Obese");
        else if (bmi > 25)
        	System.out.println("Overweight");
        else if (bmi > 18.5)
        	System.out.println("Normal");
        else
        	System.out.print("Underweight");
	}
}

(财务应用程序: 整钱兑零)修改程序淸单2-10, 使之只显示非零的币值单位,用单词的单数形式显示一个单位,例如1dollar and 1penny(1美元和1美分);用单词的复数形式显示多于一个单位的值,例如2 dollars and 3 pennies (2美元和3美分)。

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		//1)
		Scanner input = new Scanner(System.in);
		System.out.print("Please type your total money: ");
		double amount = input.nextDouble();
		System.out.println("You have " + amount + " dollars");
		//2)
		int remainingAmount = (int)(amount * 100);
		//3)
		int currentDollar = remainingAmount / 100;
		remainingAmount = remainingAmount % 100;
		//4)
		int currentQuarter = remainingAmount / 25;
		remainingAmount = remainingAmount % 25;
		//5)
		int currentDime = remainingAmount / 10;
		remainingAmount = remainingAmount % 10;
		//6)
		int currentNickel = remainingAmount / 5;
		remainingAmount = remainingAmount % 5;
		
		if (currentDollar > 1) {
			System.out.println("	consist of " + currentDollar + " dollars");
		}
		else if (currentDollar == 1) {
			System.out.println("	consist of " + currentDollar + " dollar");
		}
		
		if (currentQuarter > 1) {
			System.out.println("	consist of " + currentQuarter + " quarters");
		}
		else if (currentQuarter == 1) {
			System.out.println("	consist of " + currentQuarter + " quarter");
		}
		
		if (currentDime > 1) {
			System.out.println("	consist of " + currentDime + " dimes");
		}
		else if (currentDime == 1) {
			System.out.println("	consist of " + currentDime + " dime");
		}
		
		if (currentNickel > 1) {
			System.out.println("	consist of " + currentNickel + " nickels");
		}
		else if (currentNickel == 1) {
			System.out.println("	consist of " + currentNickel + " nickel");
		}
		
		if (remainingAmount > 1) {
			System.out.println("	consist of " + remainingAmount + " pennies");
		}
		else if (remainingAmount == 1) {
			System.out.println("	consist of " + remainingAmount + " penny");
		}
		input.close();
	}
}

(对三个整數排序)编写程序,提示用户输入三个整数。以非降序的形式显示这三个整数

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input 3 different integers: ");
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		//3 2 1
		if (a > b) {
			//x = 3
			int x = a;
			//a = 2
			a = b;
			//b = 3
			b = x;
		}
		//2 3 1
		if (a > c) {
			//y = 2;
			int y = a;
			//a = 1
			a = c;
			//c = 2
			c = y;
		}
		//1 3 2
		if (b > c) {
			//z = 3
			int z = b;
			//b = 2
			b = c;
			//c = 3
			c = z;
		}
		System.out.println(a + " " + b + " " + c);
	}
}

(商业:检查ISBN-10)ISBN-10(国际标准书号)以前是一个10位整数, 最后的一位 d 10 d_{10} d10是校验和,它是使用下面的公式用另外 9 个数计算出来的:

( d 1 ∗ 1 + d 2 ∗ 2 + d 3 ∗ 3 + d 4 ∗ 4 + d 5 ∗ 5 + d 6 ∗ 6 + d 7 ∗ 7 + d 8 ∗ 8 + d 9 ∗ 9 ) (d_1 * 1 + d_2 * 2 + d_3 * 3 + d_4 * 4 + d_5 * 5 + d_6 * 6 + d_7 * 7 + d_8 * 8 + d_9 * 9) (d11+d22+d33+d44+d55+d66+d77+d88+d99) % 11 11 11

如果校验和为10, 那么按照1SBN-10的习惯,最后一位应该表示为 X。编写程序,提示用户输入前9个数,然后显示10位 ISBN(包括前面起始位置的0)。程序应该读取一个整数输入

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the first 9 digits of ISBN-10: ");
		int a1 = input.nextInt();
		int a2 = input.nextInt();
		int a3 = input.nextInt();
		int a4 = input.nextInt();
		int a5 = input.nextInt();
		int a6 = input.nextInt();
		int a7 = input.nextInt();
		int a8 = input.nextInt();
		int a9 = input.nextInt();
		
		if ((a1 + a2 * 2 + a3 * 3 + a4 * 4 + a5 * 5 + a6 * 6 + a7 * 7 + a8 * 8 + a9 * 9) % 11 == 10)
			System.out.println("The ISBN-10 is " + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + "X");
		else
		System.out.print("The ISBN-10 is " + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8+ a9 + 
						((a1 + a2 * 2 + a3 * 3 + a4 * 4 + a5 * 5 + a6 * 6 + a7 * 7 + a8 * 8 + a9 * 9) % 11));
	}
}

(游戏:加法测验)程序淸单3-3随机产生一个减法问題。修改这个程序,随机产生一个计算两个小于 100 的整数的加法问题

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int number1 = (int)(Math.random() * 100);
		int number2 = (int)(Math.random() * 100);
		int correct = number1 + number2;
		Scanner input = new Scanner(System.in);
		System.out.print(number1 + " + " + number2 + " = ");
		int answer = input.nextInt();
		if (answer == correct) {
			System.out.print("You are correct, " + number1 + " + " + number2 + " = " + correct);
		}
		else
			System.out.print("You are wrong, the answer should be " + number1 + " + " + number2 + " = " + correct);
	}
}

(给出一个月的总天教)编写程序,提示用户输入月份和年份,然后显示这个月的天数。例如: 如果用户输人的月份是2而年份是2012,那么程序应该显示 “February 2012 has 29 days"(2012年2月有29天)。如果用户输人的月份为3而年份为2015,那么程序就应该显示 “March 2015 has 31 days”(2015年3月有31天)

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Please input number for month and year: ");
		int month = input.nextInt();
		int year = input.nextInt();
		//if可以被嵌套在switch语句中
		switch (month) {
		case 1:
			System.out.print("January " + year  + " has 31 days");
			break;
		case 2:
			if(year % 4 == 0) {
				System.out.print("February " + year  + " has 29 days");
				break;
			}
			else {
				System.out.print("February " + year  + " has 28 days");
				break;
			}
		case 3:
			System.out.print("March " + year  + " has 31 days");
			break;
		case 4:
			System.out.print("April " + year  + " has 30 days");
			break;
		case 5:
			System.out.print("May " + year  + " has 31 days");
			break;
		case 6:
			System.out.print("June " + year  + " has 30 days");
			break;
		case 7:
			System.out.print("July " + year  + " has 31 days");
			break;
		case 8:
			System.out.print("August " + year  + " has 31 days");
			break;
		case 9:
			System.out.print("September " + year  + " has 30 days");
			break;
		case 10:
			System.out.print("October " + year  + " has 31 days");
			break;
		case 11:
			System.out.print("November " + year  + " has 30 days");
			break;
		case 12:
			System.out.print("December " + year  + " has 31 days");
			break;
		}
	}
}

(回文数字)编写一个程序,提示用户输人一个三位的整数,然后确定它是否回文数字。当从左到右,以及从右到左都是一样的话,这个数字称为回文数

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input 3 different numbers: ");
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		if (a == c) 
			System.out.print("" + a + b + c + " is a palindrome");
		else
			System.out.print(""+ a + b + c + " is not a palindrome");
	}
}

(财务应用程序:计算税款)程序淸单3-5给出了计算单身登记人税款的源代码。将程序清单3-5补充完整,从而计算所有登记的婚姻状态的税款

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Type 0 for single filer, 1 for married jointly or qualifying widow, 2 for married seperately, 3 for head of household: ");
		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.1;
			else if (income <= 33950)
				tax = 8350 * 0.1 + (income - 8350) * 0.15;
			else if (income <= 82250)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
			else if (income <= 171550)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
			else if (income <= 372950)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
			else
				tax = 8350 * 0.1 + (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.1;
			else if (income <= 67900)
				tax = 16700 * 0.1 + (income - 16700) * 0.15;
			else if (income <= 137050)
				tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
			else if (income <= 208850)
				tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
			else if (income <= 372950)
				tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
			else
				tax = 16700 * 0.1 + (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.1;
			else if (income <= 33950)
				tax = 8350 * 0.1 + (income - 8350) * 0.15;
			else if (income <= 68525)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
			else if (income <= 104425)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
			else if (income <= 186475)
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
			else
				tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
		}
		
		else if (status == 3) {
			if (income <= 11950)
				tax = income * 0.1;
			else if (income <= 45500)
				tax = 11950 * 0.1 + (income - 11950) * 0.15;
			else if (income <= 117450)
				tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
			else if (income <= 190200)
				tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
			else if (income <= 372950)
				tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
			else
				tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
		}
		
		else
			System.out.print("Invalid input");
		
		System.out.println("Tax is " + (int)(tax * 100) / 100.0);
	}
}

(游戏:猜硬币的正反面)编写程序,让用户猜一猜是硬币的正面还是反面。这个程序随机产生一个整数0或者1, 它们分别表示硬币的正面和反面。程序提示用户输人一个猜测值,然后报告这个猜测值是正确的还是错误的

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Type 1 for front, 0 for back: ");
		int coin = input.nextInt();
		
		int system = (Math.random() < 0.5) ? 1 : 0;
		if (system == coin)
			System.out.print("Correct");
		else
			System.out.print("Not correct");
	}
}

(游戏:彩票)修改程序清单3-8,产生三位整数的彩票。程序提示用户输入一个三位整数,然后依照下面的规则判定用户是否贏得奖金:

1)如果用户输人的所有数匹配彩票的确切顺序,奖金是10000美元

2)如果用户输人的所有数匹配彩票的所有数字,奖金是3000美元。

3)如果用户输人的其中一个数匹配彩票号码中的一个数,奖金是 1000 美元。

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Choose you 3 digits lottery: ");
		int customer = input.nextInt();
		int computer = (int)(Math.random() * 1000);

		int cus1 = customer / 100;
		int cus2 = customer % 100 / 10;
		int cus3 = customer % 10;
		int com1 = computer / 100;
		int com2 = computer % 100 / 10;
		int com3 = computer % 10;
		
		System.out.println("The lottery number is " + computer);
		System.out.println("Your chosen number is " + customer);
		if (customer == computer)
			System.out.print("You win $10000");
		//customer computer / 123 132 / 123 213 / 123 231 / 123 321 / 123 312
		//computer customer / 123 132 / 123 213 / 123 231 / 123 321 / 123 312
		else if (cus1 == com1 && cus2 == com3 && cus3 == com2)
			System.out.print("You win $3000");
		else if (cus1 == com2 && cus2 == com1 && cus3 == com3)
			System.out.print("You win $3000");
		else if (cus1 == com3 && cus2 == com1 && cus3 == com2)
			System.out.print("You win $3000");
		else if (cus1 == com3 && cus2 == com2 && cus3 == com1)
			System.out.print("You win $3000");
		else if (cus1 == com2 && cus2 == com3 && cus3 == com1)
			System.out.print("You win $3000");
		else if (cus1 == com1 || cus1 == com2 || cus1 == com3 || cus2 == com1 || cus2 == com2 || cus2 == com3 || cus3 == com1 || cus3 == com2 || cus3 == com3)
			System.out.print("You win $1000");
		else
			System.out.print("You lose");
	}
}

(随机点)编写程序,显示矩形中一个随机点的坐标。矩形中心位于(0,0)、宽100、高200。

public class Main{
	public static void main(String[] args){
		//x [-100 , 100] && y [-50 , 50]
		int xAxis = (int)(Math.random() * 201 - 100);
		int yAxis = (int)(Math.random() * 101 - 50);
		System.out.print("(" + xAxis + ", " + yAxis + ")");
	}
}

(游戏:剪刀、石头、布)编写可以玩流行的剪刀-石头-布游戏的程序。程序提示用户随机产生一个数,这个数为 0、1 或者 2, 分别表示石头、剪刀和布。程序提示用户输入值 0、1 或者 2, 然后显示一条消息,表明用户和计算机谁贏了游戏,谁输了游戏,或是打成平手。

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Type 0 for scissor, 1 for rock, 2 for paper: ");
		int gesture = input.nextInt();
		int computer = (int)(Math.random() * 3);
		//01 / 02 / 10 / 12 / 20 / 21
		if (gesture == computer)
			System.out.print("Same gesture, try again!");
		else if (gesture == 0 && computer == 1)
			System.out.print("Computer wins");
		else if (gesture == 0 && computer == 2)
			System.out.print("Player wins");
		else if (gesture == 1 && computer == 0)
			System.out.print("Player wins");
		else if (gesture == 1 && computer == 2)
			System.out.print("Computer wins");
		else if (gesture == 2 && computer == 0)
			System.out.print("Computer wins");
		else if (gesture == 2 && computer == 1)
			System.out.print("Player wins");
	}
}

(运输成本)一个运输公司使用下面的函数,根据运输重量(以磅为单位)来计算运输成本(以美元计算)。编写一个程序,提示用户输人包裹重量 ,显示运输成本。如果重量大于20, 显示一条消息 “the package cannot beshipped”

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Type weight: ");
		double weight = input.nextDouble();
		if (weight <= 1)
			System.out.print("Fees will be $3.5");
		else if (weight <= 3)
			System.out.print("Fees will be $5.5");
		else if (weight <= 10)
			System.out.print("Fees will be $8.5");
		else if (weight <= 20)
			System.out.print("Fees will be $10.5");
		else
			System.out.print("The package cannot be shipped");
	}
}

(计算三角形的周长)编写程序,读取三角形的三条边,如果输人值合法就计算这个三角形的周长;否则,显示这些输入值不合法。如果任意两条边的和大于第三边 ,那么输人值都是合法的

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Type 3 lines for a trinangle: ");
		double line1 = input.nextDouble();
		double line2 = input.nextDouble();
		double line3 = input.nextDouble();
		
		if (line1 > line2) {
			//x = 3
			double x = line1;
			//a = 2
			line1 = line2;
			//b = 3
			line2 = x;
		}
		//2 3 1
		if (line1 > line3) {
			//y = 2;
			double y = line1;
			//a = 1
			line1 = line3;
			//c = 2
			line3 = y;
		}
		//1 3 2
		if (line2 > line3) {
			//z = 3
			double z = line2;
			//b = 2
			line2 = line3;
			//c = 3
			line3 = z;
		}
		
		if (line1 + line2 > line3)
			System.out.print(line1 + line2 + line3);
		else
			System.out.print("Invalid input");
		
	}
}

(科学:风寒温度)编程练习题2.17给出计算风寒温度的公式。这个公式适用于温度在华氏-58到41之间,并且风速大于或等于2的情况。编写一个程序,提示用户输人一个温度值和一个风速值。如果输人值是合法的,那么显示风寒温度 ,否则显示一条消息,表明温度或风速是不合法数值。

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
    	System.out.print("Enter temperature(Fahrenheit, between -58 and 41), and wind spped(>= 2): ");
        double temp = input.nextDouble();
        double speed = input.nextDouble();
        if (temp >= -58 && temp <= 41 && speed >=2)
        	System.out.print("The wind chill index is " + (35.74 + 0.6215 * temp - 35.75 * Math.pow(speed, 0.16) + 0.4275 * temp * Math.pow(speed, 0.16)));
        else
        	System.out.print("Invalid input");
        input.close();
	}
}

(科学:某天是星期几)泽勒一致性是由克里斯汀 • 泽勒开发的用于计算某天是星期几的算法。这个公式是:

h = ( q + 26 ( m + 1 ) 10 + k + k 4 + j 4 + 5 j ) h = (q + \frac{26(m+1)}{10} + k + \frac{k}{4} + \frac{j}{4} + 5j) h=(q+1026(m+1)+k+4k+4j+5j) % 7 7 7

• h是一个星期中的某一天(0为星期六;1为星期天;2为星期一;3为星期二;4为星期三 ;5为星期四; 6为星期五)。
• q是某月的第几天。
• m是月份(3为三月,4为四月,12为十二月)。一月和二月分别记为上一年的13和14月。

• j是世纪数,即 y e a r 100 \frac{year}{100} 100year

• k是该世纪的第几年(即year % 100 )。

注意,公式中的除法执行一个整数相除。编写程序,提示用户输入年 、月和该月的哪一天,然后显示它是一周中的星期几

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
    	System.out.print("Enter year: ");
    	int year = input.nextInt();
    	System.out.print("Enter month: ");
    	int m = input.nextInt();
    	System.out.print("Enter day: ");
    	int q = input.nextInt();
    	
    	if (m == 1) {
    		m = 13;
    		year = year - 1;
        	double j = year / 100;
        	int k = year % 100;
    		int h = (int)((q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7);
    		System.out.println(h);
    	}
    	else if (m == 2) {
    		m = 14;
    		year = year - 1;
        	double j = year / 100;
        	int k = year % 100;
    		int h = (int)((q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7);
    		System.out.println(h);
    	}
    	else if (m > 2) {
        	double j = year / 100;
        	int k = year % 100;
    		int h = (int)((q + (26 * (m + 1)) / 10 + k + k / 4 + j / 4 + 5 * j) % 7);
    		System.out.println(h);
    	}
        input.close();
	}
}

(几何:点是否在圆内?)编写程序,提示用户输人一个点(x,y), 然后检査这个点是否在以原点(0, 0)为圆心、半径为10的圆内

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input an x point and a y point: ");
		double x = input.nextDouble();
		double y = input.nextDouble();
		
		if (Math.pow((Math.pow(x, 2) + Math.pow(y, 2)), 0.5) <= 10)
			System.out.println("The point is in the circle");
		else
			System.out.println("The point is not in the circle");
	}
}

(几何:点是否在矩形内?)编写程序,提示用户输入点(x,y),然后检测该点是否在以原点(0,0)为中心,宽为10、高为5的矩形中

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input an x point and a y point: ");
		double x = input.nextDouble();
		double y = input.nextDouble();
		
		if (Math.abs(x) <= 5 && Math.abs(y) <= 2.5)
			System.out.println("The point is in the square");
		else
			System.out.println("The point is not in the square");
	}
}

(游戏:挑一张牌)编写程序,模拟从一副52张的牌中选择一张牌。程序应该显示牌的大小(Ace、2、3、4、5、6、7、8、9、10、Jack、Queen、King)以及牌的花色(Clubs(黑梅花)、Diamonds (红方块)、Hearts (红心)、Spades (黑桃))

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int x = (int)(Math.random() * 52 + 1);
		if (x <= 13) 
			System.out.print("The card you picked is " + x + " of Clubs.");
		else if (x <= 26) {
			x = x - 13;
			System.out.print("The card you picked is " + x + " of Diamonds.");
		}
		else if (x <= 39) {
			x = x - 26;
			System.out.print("The card you picked is " + x + " of Hearts.");
		}
		else {
			x = x - 39;
			System.out.print("The card you picked is " + x + " of Spades.");
		}
	}
}

(几何 交点)第一条直线上面的两个点是(x1,y1)和(x2,y2),第二条直线的两个点是(x3, y3)和(x4,y4), 两条直线的交点这个线性方程组可以应用Cramer规则求解(见编程练习题3.3)。如果方程无解,则两条直线平行

编写一个程序,提示用户输人这四个点,然后显示它们的交点

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input 4 points: ");
		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 a = y1 - y2;
		double b = x2 - x1;
		double c = y3 - y4;
		double d = x4 - x3;
		double e = (y1 - y2) * x1 - (x1 - x2) * y1;
		double f = (y3 - y4) * x3 - (x3 - x4) * y3;
		double x = (e * d - b * f) / (a * d - b * c);
		double y = (a * f - e * c) / (a * d - b * c);
		if (a * d - b * c == 0)
			System.out.print("The two lines are parallel");
		else
			System.out.print("The intersecting point is at (" + x + ", " + y + ")");
	}
}

(使用操作符 &&、||和^) 编写一个程序,提示用户输入一个整数值,然后判定它是否能被5和6整除,是否能被5或6整除,以及是否能被5或6整除但是不能同时被它们整除

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input an integer: ");
		int x = input.nextInt();
		boolean a = x % 5 == 0 && x % 6 == 0;
		boolean b = x % 5 == 0 || x % 6 == 0;
		boolean c = x % 5 == 0 ^ x % 6 == 0;
		
		System.out.println("Is " + x + " divisible by 5 and 6? " + a);
		System.out.println("Is " + x + " divisible by 5 or 6? " + b);
		System.out.print("Is " + x + " divisible by 5 or 6, but not both? " + c);
	}
}

(几何:点是否在三角形内?)假设一个直角三角形放在一个平面上,直角点在(0, 0)处,其他两个点分别在(200,0) 和(0,100)处。编写程序,提示用户输人一个点的x坐标和y坐标 ,然后判定这个点是否在该三角形内

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input a point: ");
		double x4 = input.nextDouble();
		double y4 = input.nextDouble();
		
		double x1 = 0;
		double y1 = 0;
		double x2 = 200;
		double y2 = 0;
		double x3 = 0;
		double y3 = 100;		
		//S = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2
		double s123 = Math.abs(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)) / 2;
		double s124 = Math.abs(x1*(y2-y4) + x2*(y4-y1) + x4*(y1-y2)) / 2;
		double s134 = Math.abs(x1*(y4-y3) + x4*(y3-y1) + x3*(y1-y4)) / 2;
		double s234 = Math.abs(x4*(y2-y3) + x2*(y3-y4) + x3*(y4-y2)) / 2;
		System.out.println(s124 + s134 + s234);
		System.out.println(s123);
    //注意浮点数运算并不是一个精确的结果,不可以直接比较
		if (Math.abs(s124 + s134 + s234 - s123) < 0.1)
			System.out.print("The point is in the triangle");
		else
			System.out.print("The point is not in the triangle");
	}
}

(几何:两个矩形)编写一个程序,提示用户输入两个矩形中点的x坐标和y坐标以及它们的宽度和高度,然后判定第二个矩形是在第一个矩形内,还是和第一个矩形重叠

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
	    /*三种情况:
		一个矩形(中心(x2,y2))在另一个(中心(x1,y1))之内:两个中心距离在x、y上的分量假设为x0、y0,中心(x1,y1)的高为h1、宽为w1,中心(x2,y2)的高为h2、宽为w2,则此时(x0 + w2 / 2) < (w1 / 2),且(y0 + h2 / 2) < (h1 / 2)
		重叠(变量用1中的):((w1 - w2) / 2) < x0 < ((w1 + w2) / 2) 且 ((h1 - h2) / 2) < y0 < ((h1 + h2) / 2)
		相离:x0 > ((w1 + w2) / 2) 或 y0 > ((h1 + h2) / 2)
		*/
		 Scanner input = new Scanner(System.in);
	        System.out.print("Enter r1's center x-. y- coordinates, width, and height: ");
	        double x1 = input.nextDouble(), y1 = input.nextDouble();
	        double w1 = input.nextDouble(), h1 = input.nextDouble();
	        System.out.print("Enter r2's center x-, y- coordinates, width, and height: ");
	        double x2 = input.nextDouble(), y2 = input.nextDouble();
	        double w2 = input.nextDouble(), h2 = input.nextDouble();

	        // 计算两个中心之间距离的分量
	        double x0 = Math.abs(x2 - x1);
	        double y0 = Math.abs(y2 - y1);

	        // 判断位置
	        if(((x0 + w2 / 2) <= (w1 / 2)) && ((y0 + h2 / 2) <= (h1 / 2))) {
	            System.out.println("r2 is inside r1");
	        }else if(((w1 - w2) / 2) < x0 && x0 < ((w1 + w2) / 2) && ((h1 - h2) / 2) < y0 && y0 < ((h1 + h2) / 2))
	            System.out.println("r2 overlaps r1");
	        else
	            System.out.println("r2 does not overlap r1");
	}
}

(几何:两个圆)编写程序,提示用户输人两个圆的中心坐标和各自的半径值,然后决定第二个 圆是在第一个圆内,还是和第一个圆重叠

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		 	Scanner input = new Scanner(System.in);
	        System.out.print("Enter c1's center x-. y- coordinates, and radius: ");
	        double x1 = input.nextDouble();
	        double y1 = input.nextDouble();
	        double r1 = input.nextDouble();
	        System.out.print("Enter c2's center x-. y- coordinates, and radius: ");
	        double x2 = input.nextDouble();
	        double y2 = input.nextDouble();
	        double r2 = input.nextDouble();
	        double d = Math.pow((Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), 0.5);
	        if (d <= r1 - r2) 
	        	System.out.print("circle2 is inside circle1");
	        
	        else if (d <= r2 - r1) 
	        	System.out.print("circle1 is inside circle2");
	        
	        else if (d > Math.abs(r1 - r2) && d < r1 + r2) 
	        	System.out.print("circle2 overlaps circlel");
	        
	        else
	        	System.out.print("circle2 does not overlaps circle1");
	}
}

(当前时间)修改编程练习题2.8, 以12小时时钟制显示小时数

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		//读取当前毫秒时间
		long totalMilliseconds = System.currentTimeMillis();
		//计算总秒数
		long totalSeconds = totalMilliseconds / 1000;
		
		//计算当前秒数
		long currentSeconds = totalSeconds % 60;
		//计算总分钟数
		long totalMinutes = totalSeconds / 60;

		//计算当前分钟数
		long currentMinutes = totalMinutes % 60;
		//计算总小时数
		long totalHours = totalMinutes / 60;

		//计算当前小时数
		long currentHours = totalHours % 24;
		
		Scanner input = new Scanner(System.in);
		System.out.println("Please input your local timeZone compared to GMT with +/- integers: ");
		int timeZone = input.nextInt();
		long newHours = currentHours + timeZone;
		if (newHours >= 24)
			newHours = newHours - 24;
		else if (newHours >= 12)
			newHours = newHours - 12;
		System.out.print("The current time is " + newHours + ":" + currentMinutes + ":" + currentSeconds);
	}
}

(金融:货币兑換)编写一个程序,提示用户输入从美元到人民币的兑换汇率。提示用户输入0表示从美元兑换为人民币 ,输入1表示从人民币兑换为美元。继而提示用户输入美元数量或者人民币数量,分别兑换为另外一种货币

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the exchange rate from dollars to RMB: ");
		double rate = input.nextDouble();
		System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
		int type = input.nextInt();
		if (type == 0) {
			System.out.print("Enter the dollar amount: ");
			double money = input.nextDouble();
			System.out.print("$" + money + " is " + money * rate + " yuan");
		}
		else if (type == 1) {
			System.out.print("Enter the RMB amount: ");
			double money = input.nextDouble();
			System.out.print(money + " yuan is " + money / rate + " dollars");
		}
		else
			System.out.print("Incorrect Input");
	}
}

(几何: 点的位置)给定一个从点p0(x0, y0)到p1(x1, p1)的有向线段, 可以使用下面的条件来确定点p2(x2, y2)是在线段的左侧 、 右侧,或者在该直线上

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter three points for pO, p1, and p2: ");
		double x0 = input.nextDouble();
		double y0 = input.nextDouble();
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();

		if ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) > 0) 
			System.out.print("(" + x2 + ", " + y2 + ")" + " is on the left side of the line from " + "(" + x0 + ", " + y0 + ") to " + "(" + x1 + ", " + y1 + ")" );
		else if ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0) 
			System.out.print("(" + x2 + ", " + y2 + ")" + " is on the line from " + "(" + x0 + ", " + y0 + ") to " + "(" + x1 + ", " + y1 + ")" );
		else
			System.out.print("(" + x2 + ", " + y2 + ")" + " is on the right side of the line from " + "(" + x0 + ", " + y0 + ") to " + "(" + x1 + ", " + y1 + ")" );
	}
}

(金融:比较成本)假设你要通过两种不同的包裹运输大米。你将乐于编写一个程序来比较成本, 该程序提示用户输入每个包裹的重童和价格,然后显示具有更好价格的包裹

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter weight and price for package 1: ");
		double weight1 = input.nextDouble();
		double price1 = input.nextDouble();
		System.out.print("Enter weight and price for package 2: ");
		double weight2 = input.nextDouble();
		double price2 = input.nextDouble();
		
		if (price2 > price1)
			System.out.print("Package 1 has a better price.");
		else if (price1 > price2)
			System.out.print("Package 2 has a better price.");
		else
			System.out.print("Two packages have the same price.");
	}
}

(几何: 线段上的点)编程练习题3.32显示了如何测试一个点是否在一个无限长的直线上。修改编程练习题3.32, 测试一个点是否在一个线段上。编写一个程序,提示用户输入三个点p0、p1和p2, 显示p2是否在从p0到p1的线段上

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter three points for pO, p1, and p2: ");
		double x0 = input.nextDouble();
		double y0 = input.nextDouble();
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		
		if ((x2 >= Math.min(x0, x1) && (x2 <= Math.max(x0, x1))) && (y2 >= Math.min(y0, y1) && (y2 <= Math.max(y0, y1)))) {
			if ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0) {
				System.out.print("(" + x2 + ", " + y2 + ")" + " is on the line from " + "(" + x0 + ", " + y0 + ") to " + "(" + x1 + ", " + y1 + ")");
			}	
		}
		else System.out.print("(" + x2 + ", " + y2 + ")" + " is NOT on the line from " + "(" + x0 + ", " + y0 + ") to " + "(" + x1 + ", " + y1 + ")");
	}
}
  • 9
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值