Java Chapter 3 exercises

Analysis Value: a b c delt solution Steps: 1. Prompt for a b c 2. Calculate the value of delt 3. Solution of equation by...

Analysis

Value: a b c delt solution

Steps:

1. Prompt for a b c

2. Calculate the value of delt

3. Solution of equation by formula

import java.util.Scanner; class Demo3_1{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("Please input a b c Value:"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double delt=b*b-4*a*c; if(delt>0){ double r1=(-b+Math.sqrt(delt))/(2*a); double r2=(-b-Math.sqrt(delt))/(2*a); System.out.printf("r1=%.2f,r2=%.2f",r1,r2); }else if(delt==0){ double r=(-b+Math.sqrt(delt))/(2*a); System.out.printf("r=%.2f",r); }else{ System.out.println("No real root!"); } } }

Analysis

Numerical value: constant x value y value a*d-b*c in equations

Steps:

1. Prompt the user for the value of abcdef

2. Calculate the value of x y according to the formula

3. Calculate a*d-b*c value

4. Print results according to a*d-b*c value

import java.util.Scanner; class Demo3_2{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Please input a,b,c,d,e,f Value:"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double d = scanner.nextDouble(); double e = scanner.nextDouble(); double f = scanner.nextDouble(); double x = (e*d-b*f)/(a*d-b*c); double y = (a*f-e*c)/(a*d-b*c); double i = a*d-b*c; if(i == 0){ System.out.print("No solution to the equation"); }else{ System.out.printf("x=%.2f,y=%.2f",x,y); } } }

Analysis:

Value: today is the day of the week and the next few days

Steps:

1. Prompt for the day of the week

2. Prompt for next few days

3. According to the formula, what day of the week is the next few days

4. Change the number of weeks to character output

import java.util.Scanner; class Demo3_3{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("Please enter the day of the week:"); int today = scanner.nextInt(); System.out.println("In the next few days:"); int future = scanner.nextInt(); int futureDay = (today+future)%7; String todayStr = ""; String futureDayStr = ""; if(today==0){ todayStr="Sunday"; }else if(today==1){ todayStr="Monday"; }else if(today==2){ todayStr="Tuesday"; }else if(today==3){ todayStr="Wednesday"; }else if(today==4){ todayStr="Thursday"; }else if(today==5){ todayStr="Friday"; }else if(today==6){ todayStr="Saturday"; } switch(futureDay){ case 0: futureDayStr="Sunday"; break; case 1: futureDayStr="Monday"; break; case 2: futureDayStr="Tuesday"; break; case 3: futureDayStr="Wednesday"; break; case 4: futureDayStr="Thursday"; break; case 5: futureDayStr="Friday"; break; case 6: futureDayStr="Saturday"; break; } System.out.println("Today is"+todayStr+"Future"+future+"Queen of heaven is"+futureDayStr); } }

Analysis:

Value: enter a number to find the reverse order number

Steps:

1. Prompt user for a number

2. Using the operation of remainder and division to find the number on each bit

3. Find out the reverse number

4. Compare the numbers with the numbers in reverse order

5. Equality is palindrome, otherwise it is not

import java.util.Scanner; class Demo3_4{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Please enter a three digit number"); int num = scanner.nextInt(); int temp=num; int sum = 0; sum = sum*10+num%10; num/=10; sum = sum*10+num%10; num/=10; sum = sum*10+num%10; if(sum==temp){ System.out.println("This number is palindrome number!"); }else{ System.out.println("This number is not palindrome number!"); } } }

Analysis:

Value: a random two digit number, a two digit number entered by the user, the number obtained by splitting the two digits of the computer and the user

Steps:

1. Let the program randomly generate a two digit number

2. Prompt user for a two digit number

3. Split the computer and the user's two digits

4. Make a digital comparison

5. Print results

import java.util.*; class Demo3_5{ public static void main(String[] args){ Random random = new Random(); int com = random.nextInt(90)+10; Scanner scanner = new Scanner(System.in); System.out.println("Please enter a two digit integer:"); int user = scanner.nextInt(); int a = com % 10; int b = com / 10; int c = user % 10; int d = user / 10; if(com==user){ System.out.print("Congratulations on your $10000 bonus!"); }else if(a==d&&b==c){ System.out.print("Congratulations on your $3000 bonus!"); }else if(a==c || a==d || b==c || b==d){ System.out.print("Congratulations on your $1000 bonus!"); }else{ System.out.print("I'm sorry that you didn't win the prize!"); } } }

Analysis:

Value: the user enters an integer of 0-2. The computer randomly generates an integer of 0-2. The scissors are 0. The stone is 1. The cloth is 2

Steps:

1. Prompt user for an integer of 0-2

2. The computer randomly generates an integer of 0-2

3. Compare two numbers and win

Tie com==user

user=0 com=2 | user=1 com=0 | user=2 com=1

User input the rest is user input

import java.util.Scanner; import java.util.Random; class Demo3_6{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Please input: scissors 0, stone 1, cloth 2:"); int user = scanner.nextInt(); Random random = new Random(); int com = random.nextInt(3); String userStr =""; String comStr =""; switch(user){ case 0: userStr="scissors"; break; case 1: userStr="stone"; break; case 2: userStr="cloth"; break; } switch(com){ case 0: comStr="scissors"; break; case 1: comStr="stone"; break; case 2: comStr="cloth"; break; } if(user==com){ System.out.printf("User is%s,Computer is%s,It ends in a draw",userStr,comStr); }else if(user==0&&com==2 || user==1&&com==0 || user==2&&com==1){ System.out.printf("User is%s,Computer is%s,User win",userStr,comStr); }else{ System.out.printf("User is%s,Computer is%s,User transmission",userStr,comStr); } } }

Analysis:

Value: day, day, month, year entered

Steps:

1. Remind the user to enter date

2. Deal with January and February according to the questions

3. Calculated according to formula

4. Print results with text

import java.util.Scanner; class Demo03_07{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); //1. Enter year first System.out.print("Please enter the year:"); int year=scanner.nextInt(); //2. In January and February of the input month, use 1314 instead of year-1 System.out.print("Please enter the month:"); int month=scanner.nextInt(); //3. Enter date System.out.print("Please enter the date:"); int day=scanner.nextInt(); //4. Handle special January and February if(month==1||month==2){ month+=12; year-=1; } //5. sets of formulas int h=(day+26*(month+1)/10+year%100+year%100/4+year/100/4+5*year/100)%7; switch(h){ case 0: System.out.println("It's Saturday."); break; case 1: System.out.println("It's Sunday."); break; case 2: System.out.println("It's Monday."); break; case 3: System.out.println("It's Tuesday."); break; case 4: System.out.println("It's Wednesday."); break; case 5: System.out.println("It's Thursday."); break; case 6: System.out.println("It's Friday."); break; } } }

Analysis:

Numerical value: center coordinate, radius of circle, coordinate of input point

Steps:

1. Prompt for coordinates of a point

2. Calculate the distance from the point to the center of the circle

3, Compare the radius of circles with the distance

4. Output the position of the point

import java.util.Scanner; class Demo3_8{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Please enter the coordinates of a point x y: "); double x = scanner.nextDouble(); double y = scanner.nextDouble(); double x0 = 0; double y0 = 0; double radius = 10; double distance = Math.sqrt(Math.pow((x0-x),2)+Math.pow((y0-y),2)); if(distance==radius){ System.out.print("Points on the circle"); }else if(distance>radius){ System.out.print("Points outside the circle."); }else{ System.out.print("Points in the circle"); } } }

import java.util.Scanner; class Demo03_09{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); //1. Prompt the user to enter the coordinates of a point System.out.print("Please enter a coordinate:"); double x=scanner.nextDouble(); double y=scanner.nextDouble(); //2. First roughly judge the range of coordinates //3. Judge the range of coordinates accurately if(x>=0&&x<=200&&y<=-0.5*x+100){ System.out.println("Point inside triangle"); }else{ System.out.println("Point outside triangle"); } } }

import java.util.*; class Demo3_10{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Please enter the center coordinates of the first rectangle x,y And length, height:"); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double w1 = scanner.nextDouble(); double h1 = scanner.nextDouble(); System.out.print("Please enter the center coordinates of the second rectangle x,y And length, height:"); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); double w2 = scanner.nextDouble(); double h2 = scanner.nextDouble(); //Calculate the range of the second rectangular center point double inMinX = x1-(w1-w2)/2; double inMaxX = x1+(w1-w2)/2; double inMinY = y1-(h1-h2)/2; double inMaxY = y1+(h1-h2)/2; double outMinX = x1-(w1+w2)/2; double outMaxX = x1+(w1+w2)/2; double outMinY = y1-(h1+h2)/2; double outMaxY = y1+(h1+h2)/2; //Determine whether it is inside or outside or nested if(x2>=inMinX&&x2<=inMaxX&&y2>=inMinY&&y2<=inMaxY){ System.out.println("Included"); }else if(x2>=outMaxX||x2<=outMinX||y2>=outMaxY||y2<=outMinY){ System.out.println("Out of"); }else{ System.out.println("nesting"); } } }

Analysis:

Steps:

1. Enter a number
2. Judge whether the number is 0 again
3. Exit if 0, continue if not

import java.util.Scanner; class Demo3_11{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); double sum=0; int positives=0; int negatives=0; System.out.print("Please enter several numbers:"); while(true){ int num = scanner.nextInt(); if(num!=0){ sum+=num; if(num>0){ positives++; }else{ negatives++; } }else{ break; } } if(positives+negatives==0){ System.out.println("There are no other numbers except 0!"); }else{ System.out.println("The number of positive numbers is:"+positives); System.out.println("The number of positive numbers is:"+negatives); System.out.println("The sum of the numbers is:"+sum); System.out.println("The average of the figures is:"+sum/(positives+negatives)); } } }

There is no summer on Shanyin Road Published 7 original articles, won praise 0, visited 83 Private letter follow

11 February 2020, 07:42 | Views: 3475

Add new comment

For adding a comment, please log in
or create account

0 comments