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