课后习题

3.1 SolveEquations.java

  1. import java.util.Scanner;
  2. public class SolveEquations {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Enter a,b,c: ");
  6. double a = input.nextDouble();
  7. double b = input.nextDouble();
  8. double c = input.nextDouble();
  9. //1.判别式,b^2 - 4ac > 0 ?
  10. double Discriminant = b * b - 4 * a *c;
  11. //2.定义好一元二次方程的两个根
  12. double r1 = ((-b + Math.pow(Discriminant,0.5)) / (2 * a));
  13. double r2 = ((-b - Math.pow(Discriminant,0.5)) / (2 * a));
  14. //根据判断条件,分别输出相对应的内容
  15. if (Discriminant > 0){
  16. System.out.print("The equation has two roots " + r1 +" and " + r2);
  17. }
  18. else if(Discriminant == 0){
  19. System.out.println("The equation has one root " + r1);
  20. }
  21. else {
  22. System.out.println("The euqation has no real roots");
  23. }
  24. }
  25. }

3.2 AddThreeNumbers.java

  1. import java.util.Scanner;
  2. public class AddThreeNumbers {
  3. public static void main(String[] args) {
  4. int number1 = (int) (System.currentTimeMillis() % 10);
  5. int number2 = (int) (System.currentTimeMillis() / 10 % 10);
  6. int number3 = (int) (System.currentTimeMillis() / 10 / 10 % 10);
  7. //Create a Scanner
  8. Scanner input = new Scanner(System.in);
  9. System.out.print(
  10. "What is " + number1 + " + " + number2 + " + " + number3 + " ? ");
  11. int answer = input.nextInt();
  12. System.out.println(number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " +
  13. (number1 + number2 + number3 == answer));
  14. }
  15. }

3.3 TwoTwoLinearEquation.java

  1. import java.util.Scanner;
  2. public class TwoTwoLinearEquation {
  3. public static void main(String[] args) {
  4. double a, b, c, d, e, f, x, y;
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Enter a,b,c,d,e,f : ");
  7. a = input.nextDouble();
  8. b = input.nextDouble();
  9. c = input.nextDouble();
  10. d = input.nextDouble();
  11. e = input.nextDouble();
  12. f = input.nextDouble();
  13. x = (e * d - b * f) / (a * d - b * c);
  14. y = (a * f - e * c) / (a * d - b * c);
  15. if (a*d - b * c ==0){
  16. System.out.println("The equation has no solution");
  17. }
  18. else {
  19. System.out.println("x is : " + x + " and y is : " + y);
  20. }
  21. }
  22. }

3.4 RandomMonth.java

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

3.5 FutureDate.java

  1. import java.util.Scanner;
  2. public class FutureDate {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Enter today's day: ");
  6. int today = input.nextInt();
  7. System.out.print("Enter the number of days elapsed since today: ");
  8. int elapseddays = input.nextInt();
  9. int nowadays = today + elapseddays;
  10. System.out.print("Today is " + Week(today) + " and the future day is " + Week(nowadays));
  11. }
  12. public static String Week(int day){
  13. switch (day % 7) {
  14. case 1:
  15. return "Monday";
  16. case 2:
  17. return "Tuesday";
  18. case 3:
  19. return "Wednesday";
  20. case 4:
  21. return "Thursday";
  22. case 5:
  23. return "Friday";
  24. case 6:
  25. return "Saturday";
  26. case 0:
  27. return "Sunday";
  28. default:
  29. return "error";
  30. }
  31. }
  32. }

3.6 HealthBMI.java

  1. import java.util.Scanner;
  2. public class HealthBMI {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. //提示并输入重量
  6. System.out.print("Enter weight in pounds: ");
  7. double weight = input.nextDouble();
  8. //提示并输入英尺
  9. System.out.print("Enter feet: ");
  10. double feet = input.nextDouble();
  11. //提示并输入英寸
  12. System.out.print("Enter inches: ");
  13. double inches = input.nextDouble();
  14. //final定义的变量为常量,其值不发生改变
  15. final double KILOGRAMS_PER_POUNDS = 0.45359237;
  16. final double METER_PER_FEET = 0.3048;
  17. final double METERS_PER_INCH = 0.0254;
  18. double weightInKilograms = weight * KILOGRAMS_PER_POUNDS;
  19. double heightInMeters = feet * METER_PER_FEET + inches * METERS_PER_INCH;
  20. double bmi = weightInKilograms / (heightInMeters * heightInMeters);
  21. //Display result
  22. System.out.println("BMI is " + bmi);
  23. if (bmi < 18.5) {
  24. System.out.println("Underweight");
  25. } else if (bmi < 25) {
  26. System.out.println("Normal");
  27. } else if (bmi < 30) {
  28. System.out.println("Overweight");
  29. } else {
  30. System.out.println("Obese");
  31. }
  32. }
  33. }

3.7 WholeMoneyAgainsttheZero.java

  1. import java.util.Scanner;
  2. //感觉还能进一步精简,冗余度太高了,但是现在太菜了,以后更新
  3. public class WholeMoneyAgainsttheZero {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Enter an amount in double, for example 11.56: ");
  7. double amount = input.nextDouble();
  8. //扩大100倍,便于对数据进行取余或除的操作
  9. int remainingAmount = (int) (amount * 100);
  10. //美元数
  11. int numberOfOneDollars = remainingAmount / 100;
  12. remainingAmount = remainingAmount % 100;
  13. //二角伍分
  14. int numberOfQuarters = remainingAmount / 25;
  15. remainingAmount = remainingAmount % 25;
  16. //一角
  17. int numberOfDimes = remainingAmount / 10;
  18. remainingAmount = remainingAmount % 10;
  19. //五美分
  20. int numberOfNickels = remainingAmount / 5;
  21. remainingAmount = remainingAmount % 5;
  22. //一美分
  23. int numberOfPennies = remainingAmount;
  24. System.out.print("Your amount " + amount + " consists of ");
  25. //美元
  26. if (numberOfOneDollars == 0) {
  27. System.out.print("");
  28. } else if (numberOfOneDollars == 1) {
  29. System.out.print(numberOfOneDollars + " dollar ");
  30. } else {
  31. System.out.print(numberOfOneDollars + " dollars ");
  32. }
  33. //二角伍分
  34. if (numberOfQuarters == 0) {
  35. System.out.print("");
  36. } else if (numberOfQuarters == 1) {
  37. System.out.print(numberOfQuarters + " Quarter ");
  38. } else {
  39. System.out.print(numberOfQuarters + " Quarters ");
  40. }
  41. //一角
  42. if (numberOfDimes == 0) {
  43. System.out.print("");
  44. } else if (numberOfDimes == 1) {
  45. System.out.print(numberOfDimes + " Dime ");
  46. } else {
  47. System.out.print(numberOfDimes + " Dimes ");
  48. }
  49. //五美分
  50. if (numberOfNickels == 0) {
  51. System.out.print("");
  52. } else if (numberOfNickels == 1) {
  53. System.out.print(numberOfNickels + " Nickel ");
  54. } else {
  55. System.out.print(numberOfNickels + " Nickels ");
  56. }
  57. //一美分
  58. if (numberOfPennies == 0) {
  59. System.out.print("");
  60. } else if (numberOfPennies == 1) {
  61. System.out.print(numberOfPennies + " Pennie ");
  62. } else {
  63. System.out.print(numberOfPennies + " Pennies ");
  64. }
  65. }
  66. }

3.8 SortThreeInteger.java

  1. import java.util.Scanner;
  2. //3.8
  3. public class SortThreeInteger {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("Please Enter three Integer: ");
  7. int a = input.nextInt();
  8. int b = input.nextInt();
  9. int c = input.nextInt();
  10. int[] arr = {a, b, c};
  11. for (int i = 0; i < arr.length; i++){
  12. //借用程序清单7.2 扑克牌的打乱顺序的一段代码,打乱数组
  13. int index = (int) (Math.random() * arr.length);
  14. int temp = arr[i];
  15. arr[i] = arr[index];
  16. arr[index] = temp;
  17. }
  18. for (int e : arr){
  19. System.out.print(e + " ");
  20. }
  21. }
  22. }

3.9 Inspection_ISBN_10.java

  1. import java.util.Scanner;
  2. //3.9
  3. public class Inspection_ISBN_10 {
  4. public static void main(String[] args) {
  5. //International Standard Book Number(ISBN)
  6. //提示语句1
  7. System.out.print("Enter the first 9 digits of an ISBN as integer: ");
  8. Scanner input = new Scanner(System.in);
  9. int num = input.nextInt();
  10. int d1 = num / 100000000 % 10;
  11. int d2 = num / 10000000 % 10;
  12. int d3 = num / 1000000 % 10;
  13. int d4 = num / 100000 % 10;
  14. int d5 = num / 10000 % 10;
  15. int d6 = num / 1000 % 10;
  16. int d7 = num / 100 % 10;
  17. int d8 = num / 10 % 10;
  18. int d9 = num % 10;
  19. int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 +
  20. d7 * 7 + d8 * 8 + d9 * 9) % 11;
  21. System.out.print("The ISBN-10 number is " + d1 + d2 + d3 + d4
  22. + d5 + d6 + d7 + d8 + d9);
  23. if (d10 == 10) {
  24. System.out.print("X");
  25. } else {
  26. System.out.print(d10);
  27. }
  28. }
  29. }

3.10 AddTest.java

  1. import java.util.Scanner;
  2. public class AddTest {
  3. public static void main(String[] args) {
  4. int number1 = (int) (Math.random() * 90) + 10;
  5. int number2 = (int) (Math.random() * 90) + 10;
  6. System.out.print("What is " + number1 + " + " + number2 + " ? ");
  7. Scanner input = new Scanner(System.in);
  8. int answer = input.nextInt();
  9. if (number1 + number2 == answer) {
  10. System.out.println("You are correct!");
  11. } else {
  12. System.out.println("Your answer is wrong.");
  13. System.out.println(number1 + " + " + number2 +
  14. " should be " + (number1 + number2));
  15. }
  16. }
  17. }

3.11 TotalDaysInaMonth.java

  1. import java.util.Scanner;
  2. public class TotalDaysInaMonth {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Enter month and year: ");
  6. int monthday = 0;
  7. int month = input.nextInt();
  8. int year = input.nextInt();
  9. if (month == 1 ||month == 3 || month == 5 || month == 7 || month == 8 ||
  10. month == 10 || month == 12){
  11. monthday = 31;
  12. }else if (month == 4 || month == 6 || month == 9 || month == 11){
  13. monthday = 30;
  14. }else if (month == 2){
  15. if (isLeapYear(year) == true) {
  16. monthday = 29;
  17. }
  18. else {
  19. monthday = 28;
  20. }
  21. }else {
  22. System.out.print("You input a error data!!!");
  23. }
  24. //可以用数组来省略
  25. switch (month){
  26. case 1: System.out.print("January");break;
  27. case 2: System.out.print("February");break;
  28. case 3: System.out.print("March");break;
  29. case 4: System.out.print("April");break;
  30. case 5: System.out.print("May");break;
  31. case 6: System.out.print("June");break;
  32. case 7: System.out.print("July");break;
  33. case 8: System.out.print("August");break;
  34. case 9: System.out.print("September");break;
  35. case 10: System.out.print("October");break;
  36. case 11: System.out.print("November");break;
  37. case 12: System.out.print("December");break;
  38. default:
  39. System.out.println("Error");
  40. }
  41. System.out.print(" " + year + " has " + monthday + " days ");
  42. }
  43. public static Boolean isLeapYear(int year){
  44. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
  45. return true;
  46. }else {
  47. return false;
  48. }
  49. }
  50. }

关键术语

本章小结