3.1
import java.util.Scanner;
/**
* 解一元二次方程
*/
public class T3_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a,b,c:");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double d=b*b-4*a*c;
if(d>0){
double r1=(-b+Math.pow(d,0.5))/(2*a);
double r2=(-b-Math.pow(d,0.5))/(2*a);
System.out.println("The equation has two roots "+r1+" and "+r2);
}else if(d==0){
double r=(-b)/(2*a);
System.out.println("The equation has one root "+r);
}else{
System.out.println("The equation has no real roots");
}
}
}
3.2
/**
* 将三个数相加
*/
public class T3_2 {
public static void main(String[] args) {
int number1 = (int) (System.currentTimeMillis() % 10);
int number2 = (int) (System.currentTimeMillis() / 10 % 10);
int number3 = (int) (System.currentTimeMillis() / 100 % 10);
Scanner input = new Scanner(System.in);
System.out.println("What is " + number1 + " + " + number2 + " + " + number3 + "? ");
int answer = input.nextInt();
System.out.println(
number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " +
(number1 + number2 + number3 == answer));
}
}
3.3
import java.util.Scanner;
/**
* 求解2*2线性方程
*/
public class T3_3 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a,b,c,d,e,f:");
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
double d=scanner.nextDouble();
double e=scanner.nextDouble();
double f=scanner.nextDouble();
if(a*d-b*c==0){
System.out.println("The equation has no solution");
}else{
double x=(e*d-b*f)/(a*d-b*c);
double y=(a*f-e*c)/(a*d-b*c);
System.out.println("x is "+x+" and y is "+y);
}
}
}
3.4
/**
* 随机月份
*/
public class T3_4 {
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;
case 12:
System.out.println("December");
break;
}
}
}
3.5
import java.util.Scanner;
/**
* 给出将来的日期
*/
public class T3_5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter today's day:");
int day = scanner.nextInt();
System.out.print("Enter the number of days elapsed since today:");
int days = scanner.nextInt();
System.out.println("Today is " + getDays(day) + " and the future day is " + getDays((days + day) % 7));
}
public static String getDays(int day) {
switch (day) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return null;
}
}
}
3.6
import java.util.Scanner;
public class T3_6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter weight in pounds:");
double weight = scanner.nextDouble();
System.out.print("Enter feet:");
double feet = scanner.nextDouble();//1 feet= 12inches
System.out.print("Enter inches:");
double inches = scanner.nextDouble();
final double KILOGRAMS_PER_POUND = 0.45359237;
final double METERS_PER_FEET = 0.3048;
//Compute BMI
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters=((inches/12)+feet)*METERS_PER_FEET;
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.9
import java.util.Scanner;
/**
* 检验ISBN-10
*/
public class T3_9 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int sum=0,num=1;
System.out.print("Enter the first 9 digits of an ISBN as integer:");
String isbn=scanner.next();
for(int i=0;i<9;i++){
int j=isbn.charAt(i)-'0';
sum+=j*num++;
}
System.out.println("The ISBN-10 number is "+(((sum%11)==10)?(isbn+"X"):(isbn+(sum%11))));
}
}
3.15
import java.util.Scanner;
/**
* 彩票
*/
public class T3_15 {
public static void main(String[] args) {
//Generate a lottery number
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 % 100 / 10;
int lotteryDigit3 = lottery % 10;
//Get digits from guess
int guessDigit1 = guess / 100;
int guessDigit2 = guess % 100 / 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 $10000");
} else if (((guessDigit1 == lotteryDigit1) && (guessDigit2 == lotteryDigit3)
&& (guessDigit3 == lotteryDigit2)) || ((guessDigit1 == lotteryDigit2) && (
guessDigit2 == lotteryDigit3) && (guessDigit3 == lotteryDigit1)) ||
((guessDigit1 == lotteryDigit2) && (guessDigit2 == lotteryDigit1) && (guessDigit3 == lotteryDigit3)) ||
((guessDigit1 == lotteryDigit3) && (guessDigit2 == lotteryDigit1) && (guessDigit3 == lotteryDigit2)) ||
((guessDigit1 == lotteryDigit3) && (guessDigit2 == lotteryDigit2) && (guessDigit3 == lotteryDigit1))) {
System.out.println("Match all digits: you win $3000");
} else if ((guessDigit1 == lotteryDigit1) || (guessDigit1 == lotteryDigit2) || (guessDigit1 == lotteryDigit3) ||
(guessDigit2 == lotteryDigit2) || (guessDigit2 == lotteryDigit3) ||
(guessDigit3 == lotteryDigit3)) {
System.out.println("Match one digits: you win $1000");
} else {
System.out.println("Sorry, no match");
}
}
}