课后习题
3.1 SolveEquations.java
import java.util.Scanner;public class SolveEquations { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a,b,c: "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); //1.判别式,b^2 - 4ac > 0 ? double Discriminant = b * b - 4 * a *c; //2.定义好一元二次方程的两个根 double r1 = ((-b + Math.pow(Discriminant,0.5)) / (2 * a)); double r2 = ((-b - Math.pow(Discriminant,0.5)) / (2 * a)); //根据判断条件,分别输出相对应的内容 if (Discriminant > 0){ System.out.print("The equation has two roots " + r1 +" and " + r2); } else if(Discriminant == 0){ System.out.println("The equation has one root " + r1); } else { System.out.println("The euqation has no real roots"); } }}
3.2 AddThreeNumbers.java
import java.util.Scanner;public class AddThreeNumbers { public static void main(String[] args) { int number1 = (int) (System.currentTimeMillis() % 10); int number2 = (int) (System.currentTimeMillis() / 10 % 10); int number3 = (int) (System.currentTimeMillis() / 10 / 10 % 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 TwoTwoLinearEquation.java
import java.util.Scanner;public class TwoTwoLinearEquation { public static void main(String[] args) { double a, b, c, d, e, f, x, y; Scanner input = new Scanner(System.in); System.out.print("Enter a,b,c,d,e,f : "); a = input.nextDouble(); b = input.nextDouble(); c = input.nextDouble(); d = input.nextDouble(); e = input.nextDouble(); f = input.nextDouble(); x = (e * d - b * f) / (a * d - b * c); 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); } }}
3.4 RandomMonth.java
public class RandomMonth { public static void main(String[] args) { 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; default: System.out.println("Error"); } }}
3.5 FutureDate.java
import java.util.Scanner;public class FutureDate { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter today's day: "); int today = input.nextInt(); System.out.print("Enter the number of days elapsed since today: "); int elapseddays = input.nextInt(); int nowadays = today + elapseddays; System.out.print("Today is " + Week(today) + " and the future day is " + Week(nowadays)); } public static String Week(int day){ switch (day % 7) { case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; case 0: return "Sunday"; default: return "error"; } }}
3.6 HealthBMI.java
import java.util.Scanner;public class HealthBMI { public static void main(String[] args) { Scanner input = new Scanner(System.in); //提示并输入重量 System.out.print("Enter weight in pounds: "); double weight = input.nextDouble(); //提示并输入英尺 System.out.print("Enter feet: "); double feet = input.nextDouble(); //提示并输入英寸 System.out.print("Enter inches: "); double inches = input.nextDouble(); //final定义的变量为常量,其值不发生改变 final double KILOGRAMS_PER_POUNDS = 0.45359237; final double METER_PER_FEET = 0.3048; final double METERS_PER_INCH = 0.0254; double weightInKilograms = weight * KILOGRAMS_PER_POUNDS; double heightInMeters = feet * METER_PER_FEET + inches * 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 WholeMoneyAgainsttheZero.java
import java.util.Scanner;//感觉还能进一步精简,冗余度太高了,但是现在太菜了,以后更新public class WholeMoneyAgainsttheZero { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an amount in double, for example 11.56: "); double amount = input.nextDouble(); //扩大100倍,便于对数据进行取余或除的操作 int remainingAmount = (int) (amount * 100); //美元数 int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; //二角伍分 int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; //一角 int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; //五美分 int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; //一美分 int numberOfPennies = remainingAmount; System.out.print("Your amount " + amount + " consists of "); //美元 if (numberOfOneDollars == 0) { System.out.print(""); } else if (numberOfOneDollars == 1) { System.out.print(numberOfOneDollars + " dollar "); } else { System.out.print(numberOfOneDollars + " dollars "); } //二角伍分 if (numberOfQuarters == 0) { System.out.print(""); } else if (numberOfQuarters == 1) { System.out.print(numberOfQuarters + " Quarter "); } else { System.out.print(numberOfQuarters + " Quarters "); } //一角 if (numberOfDimes == 0) { System.out.print(""); } else if (numberOfDimes == 1) { System.out.print(numberOfDimes + " Dime "); } else { System.out.print(numberOfDimes + " Dimes "); } //五美分 if (numberOfNickels == 0) { System.out.print(""); } else if (numberOfNickels == 1) { System.out.print(numberOfNickels + " Nickel "); } else { System.out.print(numberOfNickels + " Nickels "); } //一美分 if (numberOfPennies == 0) { System.out.print(""); } else if (numberOfPennies == 1) { System.out.print(numberOfPennies + " Pennie "); } else { System.out.print(numberOfPennies + " Pennies "); } }}
3.8 SortThreeInteger.java
import java.util.Scanner;//3.8public class SortThreeInteger { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please Enter three Integer: "); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int[] arr = {a, b, c}; for (int i = 0; i < arr.length; i++){ //借用程序清单7.2 扑克牌的打乱顺序的一段代码,打乱数组 int index = (int) (Math.random() * arr.length); int temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } for (int e : arr){ System.out.print(e + " "); } }}
3.9 Inspection_ISBN_10.java
import java.util.Scanner;//3.9public class Inspection_ISBN_10 { public static void main(String[] args) { //International Standard Book Number(ISBN) //提示语句1 System.out.print("Enter the first 9 digits of an ISBN as integer: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); int d1 = num / 100000000 % 10; int d2 = num / 10000000 % 10; int d3 = num / 1000000 % 10; int d4 = num / 100000 % 10; int d5 = num / 10000 % 10; int d6 = num / 1000 % 10; int d7 = num / 100 % 10; int d8 = num / 10 % 10; int d9 = num % 10; int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11; System.out.print("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9); if (d10 == 10) { System.out.print("X"); } else { System.out.print(d10); } }}
3.10 AddTest.java
import java.util.Scanner;public class AddTest { public static void main(String[] args) { int number1 = (int) (Math.random() * 90) + 10; int number2 = (int) (Math.random() * 90) + 10; System.out.print("What is " + number1 + " + " + number2 + " ? "); Scanner input = new Scanner(System.in); int answer = input.nextInt(); if (number1 + number2 == answer) { System.out.println("You are correct!"); } else { System.out.println("Your answer is wrong."); System.out.println(number1 + " + " + number2 + " should be " + (number1 + number2)); } }}
3.11 TotalDaysInaMonth.java
import java.util.Scanner;public class TotalDaysInaMonth { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month and year: "); int monthday = 0; int month = input.nextInt(); int year = input.nextInt(); if (month == 1 ||month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){ monthday = 31; }else if (month == 4 || month == 6 || month == 9 || month == 11){ monthday = 30; }else if (month == 2){ if (isLeapYear(year) == true) { monthday = 29; } else { monthday = 28; } }else { System.out.print("You input a error data!!!"); } //可以用数组来省略 switch (month){ case 1: System.out.print("January");break; case 2: System.out.print("February");break; case 3: System.out.print("March");break; case 4: System.out.print("April");break; case 5: System.out.print("May");break; case 6: System.out.print("June");break; case 7: System.out.print("July");break; case 8: System.out.print("August");break; case 9: System.out.print("September");break; case 10: System.out.print("October");break; case 11: System.out.print("November");break; case 12: System.out.print("December");break; default: System.out.println("Error"); } System.out.print(" " + year + " has " + monthday + " days "); } public static Boolean isLeapYear(int year){ if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ return true; }else { return false; } }}
关键术语
本章小结