Java语言程序设计(基础篇)(原书第10版) 第三章编程练习题
程序员文章站
2024-03-02 12:05:22
...
Java语言程序设计(基础篇)(原书第10版) 第三章编程练习题
心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。
目录
- Java语言程序设计(基础篇)(原书第10版) 第三章编程练习题
- 3-1 代数:解一元二次方程
- 3-2 游戏:三个数的加法
- 3-3 代数:求解2*2线性方程
- 3-4 随机月份
- 3-5 找到将来的日期
- 3-6 医疗应用程序:BMI
- 3-7 财务应用程序:整钱兑零
- 3-8 对三个整数排序
- 3-9 商业:检查ISBN-10
- 3-10 游戏:加法测验
- 3-11 给出一个月的总天数
- 3-12 回文数字
- 3-13 财务应用程序:计算税款
- 3-14 游戏:猜硬币的正反面
- 3-15 游戏:**
- 3-16 随机点
- 3-17 游戏:剪刀、石头、布
- 3-18 运输成本
- 3-19 计算三角形的周长
- 3-20 科学:风寒温度
- 3-21 科学:某天是星期几
- 3-22 几何:点是否在圆内?
- 3-23 几何:点是否在矩形内?
- 3-24 游戏:挑一张牌
- 3-25 几何:交点
- 3-26 使用操作符&&、||和^
- 3-27 几何:点是否在三角形内?
- 3-28 几何:两个矩形
- 3-29 几何:两个圆
- 3-30 当前时间
- 3-31 金融:货币兑换
- 3-32 几何:点的位置
- 3-33 金融:比较成本
- 3-34 几何:线段上的点
需要书籍或者相关资料可以私聊!!!
3-1 代数:解一元二次方程
import java.util.Scanner;
public class Program3_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 m = b*b - 4*a*c;
double n = -b / (2*a);
if(m < 0)
System.out.println("The equation has no real roots");
else if(m == 0)
System.out.printf("The equation has one root %.6f\n",n);
else
System.out.printf("The equation has two roots %.6f and %.6f\n",
(n + Math.pow(m, 0.5) / (2*a)),(n - Math.pow(m, 0.5) / (2*a)));
}
}
3-2 游戏:三个数的加法
import java.util.Scanner;
public class Program3_2 {
public static void main(String[] args) {
int number1 = (int)(System.currentTimeMillis() % 10);
int number2 = (int)(System.currentTimeMillis() * 7 % 10);
int number3 = (int)(System.currentTimeMillis() * 3 % 10);
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print(
"What is " + number1 + " + " + number2 + " + " + number3 + "? ");
int answer = input.nextInt();
System.out.println(
number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " +
(number1 + number2 + number3 == answer));
}
}
3-3 代数:求解2*2线性方程
import java.util.Scanner;
public class Program3_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 x1 = e*d - b*f, y1 = a*f - e*c, m = a * d - b * c;
if(Math.abs(m) < 1e-6)
System.out.println("The equation has no solution");
else
System.out.printf("x is %.1f and y is %.1f", x1/m, y1/m);
}
}
3-4 随机月份
public class Program3_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int month = (int)(Math.random() * 12 +1);
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;
case 12:System.out.println("December");
break;
}
}
}
3-5 找到将来的日期
import java.util.Scanner;
public class Program3_5 {
public static void main(String[] args) {
String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day: ");
int today = input.nextInt();
System.out.print("Enter he number of days elapsed since today: ");
int number = input.nextInt();
System.out.printf("Today is %s and the future day is %s", week[today], week[(today+number) % 7]);
}
}
3-6 医疗应用程序:BMI
import java.util.Scanner;
public class Program3_6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter weight in pounds
System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
System.out.print("Enter feet: ");
double feet = input.nextDouble();
// Prompt the user to enter height in inches
System.out.print("Enter inches: ");
double inches = input.nextDouble();
double height = feet * 12 + inches;
final double KILOGRAMS_PER_POUND = 0.45359237; // Constant
final double METERS_PER_INCH = 0.0254; // Constant
// Compute BMI
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
double bmi = weightInKilograms /
(heightInMeters * heightInMeters);
// Display result
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");
else
System.out.println("Obese");
}
}
3-7 财务应用程序:整钱兑零
import java.util.Scanner;
public class Program3_7 {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Receive the amount
System.out.print(
"Enter an amount in double, for example 11.56: ");
double amount = input.nextDouble();
int remainingAmount = (int)(amount * 100);
// Find the number of one dollars
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
// Find the number of quarters in the remaining amount
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
// Find the number of dimes in the remaining amount
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
// Find the number of nickels in the remaining amount
int numberOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
// Find the number of pennies in the remaining amount
int numberOfPennies = remainingAmount;
// Display results
System.out.println("Your amount " + amount + " consists of");
if(numberOfOneDollars > 1)
System.out.println(" " + numberOfOneDollars + " dollars");
else if(numberOfOneDollars == 1)
System.out.println(" " + numberOfOneDollars + " dollar");
if(numberOfQuarters > 1)
System.out.println(" " + numberOfQuarters + " quarters ");
else if(numberOfQuarters == 1)
System.out.println(" " + numberOfQuarters + " quarter ");
if(numberOfDimes > 1)
System.out.println(" " + numberOfDimes + " dimes");
else if(numberOfDimes == 1)
System.out.println(" " + numberOfDimes + " dime");
if(numberOfNickels > 1)
System.out.println(" " + numberOfNickels + " nickels");
else if(numberOfNickels == 1)
System.out.println(" " + numberOfNickels + " nickel");
if(numberOfPennies > 1)
System.out.println(" " + numberOfPennies + " pennies");
else if(numberOfPennies == 1)
System.out.println(" " + numberOfPennies + " pennie");
}
}
3-8 对三个整数排序
import java.util.Scanner;
public class Program3_8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
if(a <= b){ int temp = a; a = b; b = temp;}
if(b <= c){ int temp = b; b = c; c = temp;}
if(a <= b){ int temp = a; a = b; b = temp;}
System.out.printf("%d %d %d", a, b, c);
}
}
3-9 商业:检查ISBN-10
import java.util.Scanner;
public class Program3_9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as integer:");
int d = input.nextInt();
int d9 = d % 10;
d /= 10;
int d8 = d % 10;
d /= 10;
int d7 = d % 10;
d /= 10;
int d6 = d % 10;
d /= 10;
int d5 = d % 10;
d /= 10;
int d4 = d % 10;
d /= 10;
int d3 = d % 10;
d /= 10;
int d2 = d % 10;
d /= 10;
int d1 = d % 10;
int d10 = (d1*1 + d2*2 + d3*3 + d4*4 + d5*5 + d6*6 + d7*7 + d8*8 + d9*9) % 11;
if(d10 == 10)
System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4
+ d5 + d6 + d7 + d8 + d9 + 'X');
else
System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4
+ d5 + d6 + d7 + d8 + d9 + d10);
}
}
3-10 游戏:加法测验
import java.util.Scanner;
public class Program3_10 {
public static void main(String[] args) {
int num1 = (int)(Math.random() * 100);
int num2 = (int)(Math.random() * 100);
System.out.printf("What is %d + %d ? ", num1, num2);
Scanner input = new Scanner(System.in);
int ans = input.nextInt();
if (num1 + num2 == ans)
System.out.println("You are correct!");
else
System.out.println("Your answer is wrong.\n" + num1 + " + " + num2 + " should be " + (num1 + num2));
}
}
3-11 给出一个月的总天数
import java.util.Scanner;
public class Program3_11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Please enter month:");
int month = input.nextInt();
System.out.print("Please enter year:");
int year = input.nextInt();
int days;
boolean isLeapYear = false;
if((year%4 == 0 && year%100 != 0) || (year%400 == 0))
isLeapYear = true;
if(month == 2)
if(isLeapYear) days = 29;
else days = 28;
else if(month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
days = 31;
else days = 30;
switch(month){
case 1:System.out.println("January " + year + " has " + days + " days");
break;
case 2:System.out.println("February " + year + " has " + days + " days");
break;
case 3:System.out.println("March " + year + " has " + days + " days");
break;
case 4:System.out.println("April " + year + " has " + days + " days");
break;
case 5:System.out.println("May " + year + " has " + days + " days");
break;
case 6:System.out.println("June " + year + " has " + days + " days");
break;
case 7:System.out.println("July " + year + " has " + days + " days");
break;
case 8:System.out.println("August " + year + " has " + days + " days");
break;
case 9:System.out.println("September " + year + " has " + days + " days");
break;
case 10:System.out.println("October " + year + " has " + days + " days");
break;
case 11:System.out.println("November " + year + " has " + days + " days");
break;
case 12:System.out.println("December " + year + " has " + days + " days");
break;
}
}
}
3-12 回文数字
import java.util.Scanner;
public class Program3_12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a three-digit integer:");
int a = input.nextInt();
if(a%10 == a/100)
System.out.println(a + " is a palindrome");
else
System.out.println(a + " is not a palindrome");
}
}
3-13 财务应用程序:计算税款
import java.util.Scanner;
public class Program3_13 {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter filing status
System.out.print(
"(0-single filer, 1-married jointly or qualifying widow(er), "
+ "\n2-married separately, 3-head of household)\n" +
"Enter the filing status: ");
int status = input.nextInt();
// Prompt the user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
// Compute tax
double tax = 0;
if (status == 0) { // Compute tax for single filers
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;
else
tax = 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) { // Compute tax for married file jointly
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;
else
tax = 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) { // Compute tax for married separately
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 <= 186745)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
(income - 104425) * 0.33;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
(186745 - 104425) * 0.33 + (income - 186745) * 0.35;
}
else if (status == 3) { // Compute tax for head of household
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 +
(117450 - 45500) * 0.25 + (income - 117450) * 0.28;
else if (income <= 372950)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
(income - 190200) * 0.33;
else
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
(372950 - 190200) * 0.33 + (income - 372950) * 0.35;
}
else {
System.out.println("Error: invalid status");
System.exit(1);
}
// Display the result
System.out.println("Tax is " + (int)(tax * 100) / 100.0);
}
}
3-14 游戏:猜硬币的正反面
import java.util.Scanner;
public class Program3_14 {
public static void main(String[] args) {
int num = (int)(Math.random() * 2);
Scanner input = new Scanner(System.in);
System.out.print("Enter a number, 0 is front, 1 is back: ");
int a = input.nextInt();
System.out.printf("The coin is %s\n", (num == 1) ? "back" : "front");
if(a == num)
System.out.println("You are correct!");
else
System.out.println("You are wrong!");
}
}
3-15 游戏:**
import java.util.Scanner;
public class Program3_15 {
public static void main(String[] args) {
// Generate a lottery
int lottery = (int) (Math.random() * 1000);
// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();
// Get digits from lottery
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = lottery / 10 % 10;
int lotteryDigit3 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 100;
int guessDigit2 = guess / 10 % 10;
int guessDigit3 = guess % 10;
System.out.println("The lottery number is " + lottery);
// Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if ((lotteryDigit1 == guessDigit1 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit2)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit1)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit3)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit2)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit2 && lotteryDigit3 == guessDigit1))
System.out.println("Match all digits: you win $3,000");
else if ((guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3)
|| (guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3)
|| (guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3))
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}
3-16 随机点
public class Program3_16 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = (int)(Math.random()*201 - 100);
int y = (int)(Math.random()*101 - 50);
System.out.println("( " + x + "," + y + " )");
}
}
3-17 游戏:剪刀、石头、布
import java.util.Scanner;
public class Program3_17 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = (int)(Math.random()*3);
System.out.print("scissor (0), rock (1),paper (2):");
Scanner input = new Scanner(System.in);
int m = input.nextInt();
System.out.print("The computer is ");
if(n == 0)
{
System.out.print("scissor. ");
if(m == 0) System.out.println("You are scissor too. It is a draw");
if(m == 1) System.out.println("You are rock. You won");
if(m == 2) System.out.println("You are paper. You lost");
}
if(n == 1)
{
System.out.print("rock. ");
if(m == 0) System.out.println("You are scissor. You lost");
if(m == 1) System.out.println("You are rock too. It is a draw");
if(m == 2) System.out.println("You are paper. You won");
}
if(n == 2)
{
System.out.print("paper. ");
if(m == 0) System.out.println("You are scissor. You won");
if(m == 1) System.out.println("You are rock. You lost");
if(m == 2) System.out.println("You are paper too. It is a draw");
}
}
}
3-18 运输成本
import java.util.Scanner;
public class Program3_18 {
public static void main(String[] args) {
System.out.print("Enter weight (>0) of the package: ");
Scanner input = new Scanner(System.in);
double weight = input.nextDouble();
double ans;
if(Math.abs(weight - 1.0) <= 1e-6)
ans = 3.5;
else if(Math.abs(weight - 3.0) <= 1e-6)
ans = 5.5;
else if(Math.abs(weight - 10.0) <= 1e-6)
ans = 8.5;
else if(Math.abs(weight - 20.0) <= 1e-6)
ans = 10.5;
else
ans = -1;
if(ans == -1)
System.out.println("The package cannot be shipped.");
else
System.out.printf("The cost of the package is %.1f\n", ans);
}
}
3-19 计算三角形的周长
import java.util.Scanner;
public class Program3_19 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Please enter three sides of a triangle:");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
if(a+b > c && b+c > a && a+c > b)
System.out.println("The C of this triangle is " + (a+b+c));
else System.out.println("Input error");
}
}
3-20 科学:风寒温度
import java.util.Scanner;
public class Program3_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 ta = input.nextDouble();
System.out.print("Enter the wind speed (>=2) in miles per hour: ");
double v = input.nextDouble();
if(ta < -58 || ta > 41 || v < 2.0)
System.out.println("The input is invalid.");
else {
double twc = 35.74 + 0.6215 * ta - 35.75 * Math.pow(v, 0.16) + 0.4275 * ta * Math.pow(v, 0.16);
System.out.println("The wind chill index is " + twc);
}
}
}
3-21 科学:某天是星期几
import java.util.Scanner;
public class Program3_21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter year:(e.g., 2012):");
int year = input.nextInt();
System.out.print("Enter month: 1-12:");
int month = input.nextInt();
System.out.print("Enter the day of the month: 1-31:");
int q = input.nextInt();
int m;
if(month > 2) m = month;
else
{
m = month + 12;
year--;
}
int j = year / 100;
int k = year % 100;
int h = (q + 26*(m+1)/10 + k + k/4 + j/4 + 5*j) % 7;
System.out.print("Day of the week is ");
switch(h){
case 0:System.out.println("Saturday");break;
case 1:System.out.println("Sunday");break;
case 2:System.out.println("Monday");break;
case 3:System.out.println("Tuesday");break;
case 4:System.out.println("Wednesday");break;
case 5:System.out.println("Thursday");break;
case 6:System.out.println("Friday");break;
}
}
}
3-22 几何:点是否在圆内?
import java.util.Scanner;
public class Program3_22 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Please enter x and y:");
double x = input.nextDouble();
double y = input.nextDouble();
if(x*x + y*y <= 100)
System.out.println("( " + x + " , " + y + " ) is in the circle.");
else System.out.println("( " + x + " , " + y + " ) is not in the circle.");
}
}
3-23 几何:点是否在矩形内?
import java.util.Scanner;
public class Program3_23 {
public static void main(String[] args) {
System.out.print("Enter a point with two coordinates: ");
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
if(Math.abs(x) <= 5.0 && Math.abs(y) <= 2.5)
System.out.printf("Point (%.1f, %.1f) is in the rectangle\n", x, y);
else
System.out.printf("Point (%.1f, %.1f) is not in the rectangle\n", x, y);
}
}
3-24 游戏:挑一张牌
import java.util.Scanner;
public class Program3_24 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Please enter a number (1-52):");
int m = input.nextInt();
int x = m % 13;
int y = m % 4;
System.out.print("The card you picked is ");
switch(x) {
case 0:System.out.print("Ace ");break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:System.out.print(x + " ");break;
case 10:System.out.print("Jack ");break;
case 11:System.out.print("Queen ");break;
case 12:System.out.print("King ");break;
}
System.out.print("of ");
switch(y) {
case 0:System.out.println("Clubs");break;
case 1:System.out.println("Diamonds");break;
case 2:System.out.println("Hearts");break;
case 3:System.out.println("Spades");break;
}
}
}
3-25 几何:交点
import java.util.Scanner;
public class Program3_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 a = y1 - y2, b = x1 - x2;
double c = y3 - y4, d = x3 - x4;
double e = a * x1 - b * y1;
double f = c * x3 - d * y3;
double m = a * d - b * c;
if(Math.abs(m) <= 1e-6)
System.out.println("The two lines are parallel");
else{
double x = e*d - b*f, y = a*f - e*c;
System.out.printf("The intersecting point is at (%.5f, %5f)", x/m, y/m);
}
}
}
3-26 使用操作符&&、||和^
import java.util.Scanner;
public class Program3_26 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = input.nextInt();
boolean f1 = (num % 5 == 0), f2 = (num % 6 == 0);
System.out.printf("Is %d divisible by 5 and 6 ? " + (f1 && f2) + "\n", num);
System.out.printf("Is %d divisible by 5 or 6 ? " + (f1 || f2) + "\n", num);
System.out.printf("Is %d divisible by 5 or 6, but not both? " + (f1 ^ f2) + "\n", num);
}
}
3-27 几何:点是否在三角形内?
import java.util.Scanner;
public class Program3_27 {
public static void main(String[] args) {
System.out.print("Enter a point's x- and y- coordinates: ");
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
if(x <= 0 || y <= 0 || (y + x / 2.0 >= 100.0))
System.out.println("The point is not in the triangle");
else
System.out.println("The point is in the triangle");
}
}
3-28 几何:两个矩形
import java.util.Scanner;
public class Program3_28 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");
double r1X = input.nextDouble();
double r1Y = input.nextDouble();
double r1Weight = input.nextDouble();
double r1Height = input.nextDouble();
System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");
double r2X = input.nextDouble();
double r2Y = input.nextDouble();
double r2Weight = input.nextDouble();
double r2Height = input.nextDouble();
double aX = r2X + r2Weight / 2.0, aY = r2Y + r2Height / 2.0;
double bX = r2X + r2Weight / 2.0, bY = r2Y - r2Height / 2.0;
double cX = r2X - r2Weight / 2.0, cY = r2Y + r2Height / 2.0;
double dX = r2X - r2Weight / 2.0, dY = r2Y - r2Height / 2.0;
int cnt = 0;
if(aX <= r1X + r1Weight / 2.0 && aX >= r1X - r1Weight / 2.0
&& aY <= r1Y + r1Height / 2.0 && aY >= r1Y - r1Height / 2.0)
cnt++;
if(bX <= r1X + r1Weight / 2.0 && bX >= r1X - r1Weight / 2.0
&& bY <= r1Y + r1Height / 2.0 && bY >= r1Y - r1Height / 2.0)
cnt++;
if(cX <= r1X + r1Weight / 2.0 && cX >= r1X - r1Weight / 2.0
&& cY <= r1Y + r1Height / 2.0 && cY >= r1Y - r1Height / 2.0)
cnt++;
if(dX <= r1X + r1Weight / 2.0 && dX >= r1X - r1Weight / 2.0
&& dY <= r1Y + r1Height / 2.0 && dY >= r1Y - r1Height / 2.0)
cnt++;
if(cnt == 4)
System.out.println("r2 is inside r1");
else if(cnt == 0)
System.out.println("r2 does not overlap r1");
else
System.out.println("r2 overlaps r1");
}
}
3-29 几何:两个圆
import java.util.Scanner;
public class Program3_29 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter circle1's center x-, y-coordinates, and radius: ");
double c1X = input.nextDouble();
double c1Y = input.nextDouble();
double c1R = input.nextDouble();
System.out.print("Enter circle2's center x-, y-coordinates, and radius: ");
double c2X = input.nextDouble();
double c2Y = input.nextDouble();
double c2R = input.nextDouble();
double d = Math.sqrt((c1X - c2X) * (c1X - c2X) + (c1Y - c2Y) * (c1Y - c2Y));
if(d <= Math.abs(c1R - c2R))
System.out.println("circle2 is inside circle1");
else if(d <= c1R + c2R)
System.out.println("circle2 overlaps circle1");
else
System.out.println("circle2 does not overlap circle1");
}
}
3-30 当前时间
import java.util.Scanner;
public class Program3_30 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT:");
int change = input.nextInt();
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;
long Hours = (currentHours + change) % 24;
if(Hours < 12)
System.out.println("The current time is " + Hours + ":" + currentMinutes + ":" + currentSeconds + " AM");
else if(Hours == 12)
System.out.println("The current time is " + Hours + ":" + currentMinutes + ":" + currentSeconds + " PM");
else
System.out.println("The current time is " + (Hours%12) + ":" + currentMinutes + ":" + currentSeconds + " PM");
}
}
3-31 金融:货币兑换
import java.util.Scanner;
public class Program3_31 {
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 num = input.nextInt();
if(num == 0){
System.out.print("Enter the dollar amount: ");
double dollar = input.nextDouble();
System.out.printf("$%.1f is %.1f yuan\n", dollar, dollar * rate);
}else if(num == 1){
System.out.print("Enter the RMB amount: ");
double RMB = input.nextDouble();
System.out.printf("%.1f yuan is $%.1f\n", RMB, RMB / rate);
}else
System.out.println("Incorrect input");
}
}
3-32 几何:点的位置
import java.util.Scanner;
public class Program3_32 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter three points for p0, 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.printf("(%.1f, %.1f) is on the left side of the line from (%.1f, %.1f) to (%.1f, %.1f)", x2,y2,x0,y0,x1,y1);
else if((x1 - x0) * (y2 * y0) - (x2 - x0) * (y1 - y0) == 0)
System.out.printf("(%.1f, %.1f) is on the line from (%.1lf, %.1f) to (%.1f, %.1f)", x2,y2,x0,y0,x1,y1);
else
System.out.printf("(%.1f, %.1f) is on the right side of the line from (%.1f, %.1f) to (%.1f, %.1f)", x2,y2,x0,y0,x1,y1);
}
}
3-33 金融:比较成本
import java.util.Scanner;
public class Program3_33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter weight and price for package 1: ");
double w1 = input.nextDouble();
double p1 = input.nextDouble();
System.out.print("Enter weight and price for package 1: ");
double w2 = input.nextDouble();
double p2 = input.nextDouble();
double t1 = w1 / p1, t2 = w2 / p2;
if(t1 > t2)
System.out.println("Package 1 has a better price");
else if(t1 == t2)
System.out.println("Two packages have the same price");
else
System.out.println("Package 2 has a better price");
}
}
3-34 几何:线段上的点
import java.util.Scanner;
public class Program3_34 {
public static void main(String[] args) {
System.out.print("Enter three points for p0, p1, and p2: ");
Scanner input = new Scanner(System.in);
double p0X = input.nextDouble();
double p0Y = input.nextDouble();
double p1X = input.nextDouble();
double p1Y = input.nextDouble();
double p2X = input.nextDouble();
double p2Y = input.nextDouble();
if((p2X - p1X) * (p0Y - p1Y) == (p2Y - p1Y) * (p0X - p1X) && p2X >= Math.min(p0X, p1X)
&& p2X <= Math.max(p0X, p1X) && p2Y >= Math.min(p0Y, p1Y) && p2Y <= Math.max(p0Y, p1Y))
System.out.printf("(%.1f, %.1f) is on the line segment from (%.1f, %.1f) to (%.1f, %.1f)\n",
p2X, p2Y, p0X, p0Y, p1X, p1Y);
else
System.out.printf("(%.1f, %.1f) is not on the line segment from (%.1f, %.1f) to (%.1f, %.1f)\n",
p2X, p2Y, p0X, p0Y, p1X, p1Y);
}
}