//exercise 3.12
packagethirdchapterexercise2;importjava.util.Scanner;public classfirst {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("please input a integer:");int input=in.nextInt();if(input%5==0&&input%6==0)
System.out.println(input+" is divisible by both 5 and 6");else if (input %5==0^input%6==0)
System.out.println(input+" is dibisible by 5 or 6,but not both");elseSystem.out.println(input+" is not divisible by either 5 or 6");
}
}//exercise 3.14
packagethirdchapterexercise2;importjava.util.Scanner;public classsecond {public static voidmain(String[] args) {//TODO Auto-generated method stub
int random=(int)(Math.random()*100)%2;
String computer;//指定random
if (random==0){
computer="tails";
}elsecomputer="heads";
Scanner in=newScanner(System.in);
System.out.println("(please input heads of tails)");
String input=in.next();if((random==1&&input.equals("heads"))||(random==0&&input.equals("tails"))){
System.out.println("computer is "+computer);
System.out.println("You are right!");
}else{
System.out.println("computer is "+computer);
System.out.println("You are wrong!");
}
}
}//exercise 3.15
packagethirdchapterexercise2;importjava.util.Scanner;public classthird {public static voidmain(String[] args) {//lottery number
int lottery=(int)(Math.random()*1000);
Scanner in=newScanner(System.in);
System.out.print("Enter your lottery pick (three digits):");int guess =in.nextInt();//separation lottery digit
int lotteryDigit1=lottery/100;int lotteryDigit2=lottery/10%10;int lotteryDigit3=lottery%10;//separation guess digit
int guessDigit1=guess/100;int guessDigit2=guess/10%10;int guessDigit3=guess%10;
System.out.println("The lottery number is "+lottery);//judge
if (guess ==lottery)
System.out.println("Exact match:you win $10000");else if((guessDigit2==lotteryDigit1||guessDigit2==lotteryDigit3)&&(guessDigit1==lotteryDigit2||guessDigit1==lotteryDigit3)&&(guessDigit3==lotteryDigit1||guessDigit3==lotteryDigit2))
System.out.println("Match all digits: you win $3000");else if(guessDigit1==lotteryDigit1||guessDigit1==lotteryDigit2||guessDigit1==lotteryDigit3||guessDigit2==lotteryDigit1||guessDigit2==lotteryDigit2||guessDigit2==lotteryDigit3||guessDigit3==lotteryDigit1||guessDigit3==lotteryDigit2||guessDigit3==lotteryDigit3)
System.out.println("Match one digit:you win $ 1000");elseSystem.out.println("Sorry,no match");
}
}//exercise 3.16
packagethirdchapterexercise2;public classfourth {public static voidmain(String[] args) {//TODO Auto-generated method stub
int a=(int)(Math.random()*26)+'A';char b=(char)a;
System.out.println(b);
}
}//exercise 3.17
packagethirdchapterexercise2;importjava.util.Scanner;public classfifth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("scissor(0),rock(1),paper(2):");int input=in.nextInt();int computer=(int)(Math.random()*3);if(computer==0&&input==1)
System.out.print("The computer is scissor.You are rock.You win");else if (computer==0&&input==2)
System.out.print("The computer is scissor.You are paper.You 输了.");else if (computer==0&&input==0)
System.out.print("The computer is scissor.You are scissor.It is a draw.");else if (computer==1&&input==0)
System.out.print("The computer is rock.You are scissor.You 输了.");else if (computer==1&&input==1)
System.out.print("The computer is rock.You are rock.It is a draw.");else if (computer==1&&input==2)
System.out.print("The computer is rock.You are paper.You win.");else if (computer==2&&input==0)
System.out.print("The computer is paper.You are scissor.You win.");else if (computer==2&&input==1)
System.out.print("The computer is paper.You are rock.You 输了.");else if (computer==2&&input==2)
System.out.print("The computer is paper.You are paper.It is a draw.");
}
}//exercise 3.18
packagethirdchapterexercise2;importjavax.swing.JOptionPane;public classsixth {public static voidmain(String[] args) {//TODO Auto-generated method stub
String year=JOptionPane.showInputDialog("Enter a year:");int year1=Integer.parseInt(year);boolean isLeapYear=(year1%4==0&&year1%100!=0)||(year1%400==0);
JOptionPane.showMessageDialog(null,year1+"is a leap year?"+isLeapYear);
}
}//exercise 3.19
packagethirdchapterexercise2;importjava.util.Scanner;public classseventh {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter three edges:");double side1=in.nextDouble();double side2=in.nextDouble();double side3=in.nextDouble();double max=side1,sum=0;if(max
max=side2;
sum=sum+side1;
}elsesum=sum+side2;if(max
max=side3;
sum=sum+side2;
}elsesum=sum+side3;
System.out.println("Can edges "+side1+","+side2+" and "+side3+" form a "
+ "triangle?"+(sum>max));
}
}//exercise 3.20
packagethirdchapterexercise2;importjava.util.Scanner;public classeighth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.println("please input -58F to 41F of temperature and better than 2 of "
+ "wind speed!!");//input Fahrenheit
System.out.print("Enter the temperature in Fahrenheit:");double fahreheit=in.nextDouble();if(fahreheit41){
System.out.println("input error!!");
System.exit(0);
}//input wind speed
System.out.print("Enter the wind speed miles per hours:");double windspeed=in.nextDouble();if (windspeed<2){
System.out.println("input error!!");
System.exit(0);
}double windchillindex=35.74+0.6215*fahreheit-35.75*Math.pow(windspeed,0.16)+
0.4275*fahreheit*Math.pow(windspeed,0.16);
System.out.print("The wind chill index is "+windchillindex);
}
}//exercise 3.21
packagethirdchapterexercise2;importjava.util.Scanner;public classninth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter year:(e.g.m 2008):");int year=in.nextInt();
System.out.print("Enter month : 1-12:");int month=in.nextInt();
System.out.print("Enter the day of the month: 1-31:");int day=in.nextInt();if (month==1||month==2){
month=month+12;
year--;
}int q=(day+(26*(month+1)/10)+year%100+year%100/4+year/100/4+5*year/100)%7;switch(q){case 0:
System.out.println("Day of the weel is Saterday");break;case 1:
System.out.println("Day of the weel is Sunday");break;case 2:
System.out.println("Day of the weel is Monday");break;case 3:
System.out.println("Day of the weel is Tuesday");break;case 4:
System.out.println("Day of the weel is Wednesday");break;case 5:
System.out.println("Day of the weel is Thursday");break;case 6:
System.out.println("Day of the weel is Friday");break;
}
}
}//exercise 3.22
packagethirdchapterexercise2;importjava.util.Scanner;public classtenth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter a point with two coordinates:");double x=in.nextDouble();double y=in.nextDouble();double distance=Math.pow((x*x+y*y),0.5);if (distance<10){
System.out.print("Point("+x+","+y+") is in the circle ");
}elseSystem.out.print("Point("+x+","+y+") is not in the circle ");
}
}//exercise 3.23
packagethirdchapterexercise3;importjava.util.Scanner;public classfirst {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter a point with two coordinates:");double x=in.nextDouble();double y=in.nextDouble();//horizontal is x
double horizontal=x;//vertical is y
double vertical=y;if (horizontal<5&&vertical<2.5)
System.out.print("Point("+x+","+y+") is in the rectangle");elseSystem.out.print("Point("+x+","+y+") is not in the rectangle");
}
}//exercise 3.24
packagethirdchapterexercise3;public classsecond {public static voidmain(String[] args) {//TODO Auto-generated method stub
int number1=(int)(Math.random()*13);int color1=(int)(Math.random()*4);
String number=null,color=null;switch(number1){case 0:
number="Ace";break;case 1:
number="2";break;case 2:
number="3";break;case 3:
number="4";break;case 4:
number="5";break;case 5:
number="6";break;case 6:
number="7";break;case 7:
number="8";break;case 8:
number="9";break;case 9:
number="10";break;case 10:
number="Jack";break;case 11:
number="Queen";break;case 12:
number="King";break;default:break;
}switch(color1){case 0:
color="Clubs";break;case 1:
color="Diamond";break;case 2:
color="Heart";break;case 3:
color="Spades";break;
}
System.out.println("The card you picked is "+number+" of "+color);
}
}//exercise 3.25
packagethirdchapterexercise3;importjava.util.Scanner;public classthird {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter three edges:");double side1=in.nextDouble();double side2=in.nextDouble();double side3=in.nextDouble();double max=side1,sum=0;//find max and sum of two side//Judge whether existence of triangle
if(max
max=side2;
sum=sum+side1;
}elsesum=sum+side2;if(max
max=side3;
sum=sum+side2;
}elsesum=sum+side3;//calculate the perimeter
double perimeter=sum+max;if(sum
System.out.println("input is wrong");elseSystem.out.print("sum of three side is:"+perimeter);
}
}//exercise 3.27
packagethirdchapterexercise3;importjava.util.Scanner;public classfourth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter a point's x- and y-coordinates:");double x=in.nextDouble();double y=in.nextDouble();double distance1=Math.pow((x*x+y*y), 0.5);double distance2=Math.pow((x-200)*(x-200)+(y-100)*(y-100),0.5);if(distance1>distance2||x>200||x<0||y>100||y<0)
System.out.print("The point is not in the triangle");elseSystem.out.print("The point is in the triangle");
}
}//exercise 3.28
packagethirdchapterexercise3;importjava.util.Scanner;public classfifth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in =newScanner(System.in);
System.out.println("Enter r1's center x-,y-coordinates,width,and height:");double x1=in.nextDouble();double y1=in.nextDouble();double w1=in.nextDouble();double h1=in.nextDouble();
System.out.println("Enter r2's center x-,y-coordinates,width,and height:");double x2=in.nextDouble();double y2=in.nextDouble();double w2=in.nextDouble();double h2=in.nextDouble();double insidelimitwidth=Math.abs(w1-w2);double insidelimitheight=Math.abs(h1-h2);double insidejudgewidth=Math.abs(x2-x1);double insidejudgeheight=Math.abs(y2-y1);double overlapslimitwidth=w1+w2;double overlapslimitheight=h1+h2;double overlapsjudgewidth=Math.abs(x2-x1);double overlapsjudgeheight=Math.abs(y2-y1);if (insidejudgewidth
System.out.println("r2 is inside r1");else if (overlapsjudgewidth
System.out.println("r2 overlaps r1");elseSystem.out.println("r2 does not overlap r1");
}
}//exercise 3.29
packagethirdchapterexercise3;importjava.util.Scanner;public classsixth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.println("Enter circle1's center x-,y-coordinates,and radius:");double x1=in.nextDouble();double y1=in.nextDouble();double r1=in.nextDouble();
System.out.println("Enter circle2's center x-,y-coordinates,and radius:");double x2=in.nextDouble();double y2=in.nextDouble();double r2=in.nextDouble();double distance=Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5);double insidelimit=Math.abs(r1-r2);double overlapslimit=r1+r2;if(distance
System.out.println("circle2 is inside circle1");else if (distance
System.out.println("circle2 overlaps circle1");elseSystem.out.println("circle2 does not overlap circle1");
}
}