英文名:Introduction to Java Programming and Data Structures, Comprehensive Versions, 11th Edition
3.12
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("Enter a three-digit integer:");
Scanner input =newScanner(System.in);int num = input.nextInt();int old = num;if(num<0)
num*=(-1);int gewei = num%10;int baiwei = num/100;if(gewei==baiwei)
System.out.println(old+" is a palindrome.");else
System.out.println(old+" is not a palindrome.");}}
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){int coin =(int)(Math.random()*2);
System.out.print("Guess: ");
Scanner input =newScanner(System.in);int g = input.nextInt();if(g==coin)
System.out.println("You are right, coin is "+coin);else
System.out.println("You are wrong, coin is "+coin);}}
publicclassbook{publicstaticvoidmain(String[] args){int x =(int)(Math.random()*100)-50;int y =(int)(Math.random()*200)-100;
System.out.println("The random point is ("+x+","+y+")");}}
3.17
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("scissor(0), rock(1), paper(2):");
Scanner input =newScanner(System.in);int player = input.nextInt();int com =(int)(Math.random()*3);
String[] names ={"scissor","rock","paper"};if(com==player)
System.out.println("The computer is "+names[com]+". You are "+names[player]+" too. It is a draw.");else{
System.out.print("The computer is "+names[com]+". You are "+names[player]+". You ");
String result ="";if((com==0&&player==1)||(com==1&&player==2)||(com==2&&player==0))
result="win";else
result="lose";
System.out.println(result);}}}
3.18
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("Enter the weight:");
Scanner input =newScanner(System.in);double w = input.nextDouble();double c =0;if(w>20)
System.out.println("the package cannot be shipped");elseif(w<=0)
System.out.println("Invalid input");elseif(w<=1)
c=3.5;elseif(w<=3)
c=5.5;elseif(w<=10)
c=8.5;elseif(w<=20)
c=10.5;if(c!=0)
System.out.println("The cost is "+c);}}
3.19
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("Enter the three lengths:");
Scanner input =newScanner(System.in);double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double cir = a+b+c;if((2*a>=cir)||(2*b>=cir)||(2*c>=cir))
System.out.println("Invalid input");else
System.out.println("The circumference is "+cir);}}
3.20
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
Scanner input =newScanner(System.in);
System.out.print("Enter the temperature: ");double t = input.nextDouble();
System.out.print("Enter the velocity: ");double v = input.nextDouble();double twc =35.74+0.6215*t-35.75*Math.pow(v,0.16)+0.4275*t*Math.pow(v,0.16);if(t>=-58&&t<=41&v>=2)
System.out.println("The twc is "+twc);else
System.out.println("Invalid input.");}}
3.21
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){int[] mdays ={31,28,31,30,31,30,31,31,30,31,30,31};
Scanner input =newScanner(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();if((year%4==0&&year%100!=0)||(year%400==0))
mdays[1]=29;
System.out.print("Enter the day of the month: 1-"+mdays[month-1]+": ");int q = input.nextInt();if(month==1||month==2){
month+=12;
year--;}int h=(q+26*(month+1)/10+year%100+year%100/4+year/100/4+5*(year/100))%7;
String[] days ={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
System.out.println("Day of the week is "+days[h]);}}
3.22
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("Enter a point with two coordinates: ");
Scanner input =newScanner(System.in);double x = input.nextDouble();double y = input.nextDouble();if(x*x+y*y>100)
System.out.println("Point ("+x+", "+y+") is not in the circle");else
System.out.println("Point ("+x+", "+y+") is in the circle");}}
3.23
import java.util.Scanner;publicclassbook{publicstaticvoidmain(String[] args){
System.out.print("Enter a point with two coordinates: ");
Scanner input =newScanner(System.in);double x = input.nextDouble();double y = input.nextDouble();if(x<=10/2&&x>=-10/2&&y<=5.0/2&&y>=-5.0/2)
System.out.println("Point ("+x+", "+y+") is in the rectangle");else
System.out.println("Point ("+x+", "+y+") is not in the rectangle");}}