Java Language Programming. Chapter 3 Selection of Basic Chapter

Posted by erfg1 on Tue, 23 Jul 2019 12:22:32 +0200

Java Language Programming. Chapter 3 Selection of Basic Chapter

Below are some exercises in Chapter 3.

  • A single branch if statement is an action performed if and only if the condition is true. The statement is designed as follows:

If (Boolean expression){
Statements (Groups)
}

  • Double branch if-else statement:

if (Boolean expression){
Boolean expressions are statements (groups) that are executed at the same time.
} else{
Boolean expressions are statements (groups) that are executed on holidays.
}

  • The switch statement executes the statement based on the value of the variable or expression:

switch(switch expression){
case value 1: statement (group) 1;
break;
case value 2: statement (group) 2
; break;
...
case value N:
Statement (group) Nv break;
Default: Statements executed by default (groups)
}

  • The switch expression must be able to compute a char, byte, short,int, or String value, and must always be enclosed in parentheses.

3.12 (palindrome number) Write a program that prompts the user to input a three-digit integer, and then determine whether it is palindrome number. This number is called palindrome number when it is the same from left to right and from right to left.
The procedure is as follows:

import java.util.*;
class Demo03{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter a three-digit number:");
        int number = scanner.nextInt();
        if(number>999||number<100){
            System.out.println("The number entered is incorrect!");
            return ;
        }
        int numberA = number/100;
        int numberB = (number%100)%10;
        if(numberA == numberB){
            System.out.println("This number is palindrome number");
        }else{
            System.out.println("This number is not palindrome number");
        }
    }
}

3.15 (Game: Lottery) Modify List 3-8 to generate three-digit lottery tickets. The program prompts the user to enter a three-digit integer, and then decides whether the user wins the bonus according to the following rules:
1) If the number of users who lose matches the exact order of the lottery tickets, the bonus is $10,000.
2) If all the numbers the user loses match all the numbers of the lottery ticket, the bonus is $3000.
3) If one of the users loses matches one of the lottery numbers, the bonus is $1000.

import java.util.*;
class Home5{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int comnumber = (random.nextInt(90)+10)*10;
        System.out.print("Please enter three digits:");
        int number = scanner.nextInt();
        if(number>999||number<100){
            System.out.println("Input error, end of program");
            return ;
        }
        int numberA = number/100;
        int numberB = number/10%10;
        int numberC = number%10;
        int comnumberA = comnumber/100;
        int comnumberB = comnumber/10%10;
        int comnumberC = comnumber%10;
        if(number==comnumber){
            System.out.println("Congratulations, get $10,000");
        }else if((numberA==comnumberA&&numberB==comnumberC&&numberC==comnumberB)||
                (numberA==comnumberB&&numberB==comnumberA&&numberC==comnumberC)||
                (numberA==comnumberB&&numberB==comnumberC&&numberC==comnumberA)||
                (numberA==comnumberC&&numberB==comnumberA&&numberC==comnumberB)||
                (numberA==comnumberC&&numberB==comnumberB&&numberC==comnumberA)){
                    System.out.println("Congratulations, 3000 dollars");
        }else if((numberA==comnumberA)||(numberA==comnumberB)||(numberA==comnumberC)||
                (numberB==comnumberB)||(numberB==comnumberB)||(numberB==comnumberC)||
                (numberC==comnumberA)||(numberC==comnumberB)||(numberC==comnumberC)){
                    System.out.println("Congratulations, 1000 dollars");
        }else{
            System.out.println("Thank you for your participation.");
        }
    }
}

This topic mainly uses nested if-else control sentences, and pay attention to the use of Boolean expressions in the sentences; a three-digit divided by 100 is to take its first digit, divided by 10, and then to take the remaining 10 means that the second digit of this three-digit is to take out ten digits, and then use this method to take out the first digit. Its bits are taken out.

3.17 (Game: Scissors, Stone, Cloth) Write programs that can play popular scissors-Stone-Cloth games. (Scissors can cut cloth, stones can smash scissors, and cloth can cut stones.) The program prompts the user to randomly generate a number of 0, 1 or 2, representing stones, scissors and cloth, respectively. The program prompts the user to enter a value of 0, 1 or 2, and then displays a message indicating who wins the game, loses the game, or draws.

import java.util.*;
class Demo02{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        String[] arr={"scissors","stone","cloth"};
        System.out.print("Enter your information:");
        int usr = scanner.nextInt();
        int com = random.nextInt(3);
        if(usr>3||usr<0){
            System.out.println("Input error");
            return;
        }
        if(usr==com){
            System.out.println("Computers are"+arr[com]+",You are?"+arr[usr]+",It ends in a draw");
        }else if(usr-com==1||usr-com==-2){
            System.out.println("Computers are"+arr[com]+",You are?"+arr[usr]+",You win");
        }else{
            System.out.println("Computers are"+arr[com]+",You are?"+arr[usr]+",You lost");
        }
    }
}

In this topic, we still use nested if-else statements, but we need to pay attention to the point that when users start to input, they can judge whether the number they input is in the game scope and some Boolean expressions.

3.19 (Calculating the perimeter of a triangle) Write a program that reads the three sides of a triangle and calculates the perimeter of the triangle if the input value is legitimate; otherwise, it shows that the input values are not legitimate. If the sum of any two sides is greater than that of the third side, then the input value is legal.

*/
import java.util.*;
class Home07{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter three sides of a triangle:");
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        if(a+b>c&&a+c>b&&b+c>a){
            System.out.println("Trilaterals can form triangles");
        }else{
            System.out.println("Trilaterals cannot form triangles");
        }
    }
}

It should be noted that the sum of any two sides of a triangle is larger than that of the third side and the use of if-else.

3.22 (Several Kes: Is the point in the circle?) Write a program to prompt the user to input a point (x,y), and then check whether the point is in the circle with the origin (0, 0) as the center and the radius of 10. For example, (4, 5) is a point inside the circle, and (9, 9) is a point outside the circle.

import java.util.*;
class Demo04{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a pair of point coordinates:");
        double x = scanner.nextDouble();
        double y = scanner.nextDouble();
        double s = Math.sqrt(x*x+y*y);		//Calculate the distance between the coordinates of this point and the center of the circle
        if(s<10){
            System.out.println("spot("+x+","+y+")"+"It's a point in a circle.");
        }else{
            System.out.println("spot("+x+","+y+")"+"Points that are not in a circle");
        }
    }
}

The main purpose of this question is to determine whether a point is in a circle by using if-else sentences.

3.28 (Geometry: Two Rectangles) Write a program that prompts the user to input the x and y coordinates of the midpoint of two rectangles and their width and height, and then determine whether the second rectangle is in the first rectangle or overlaps the first rectangle.

import java.util.*;
class Home10{
    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 Long, high:");
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double w1 = scanner.nextDouble();
        double h1 = scanner.nextDouble();
        System.out.print("Enter the center coordinates of the second rectangle x,y Long, high:");
        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's 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");
        }
    }
}

The important thing in this problem is to calculate the coordinates of the second rectangular center point for the range of the first rectangle. The coordinates of the second rectangular center point in the first rectangle are respectively found in the upper left, upper right, lower left and lower right. The second rectangular center point is also required outside the first rectangle and is attached to the upper left and upper right of the first rectangular side. The lower left and lower right coordinates are used to determine the position of the second rectangle.

3.31 (Finance: Currency Exchange) Write a program to prompt users to enter the exchange rate from US dollar to RMB. The user is prompted to enter 0 to convert from US dollar to RMB, and input 1 to convert RMB to US dollar. The user is then prompted to enter the amount of US dollars or RMB, which is converted into another currency.

import java.util.*;
class Class11{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter the exchange rate of US dollars to Renminbi:");
        double rate = scanner.nextDouble();
        System.out.print("Enter 0 to choose US dollars and 1 to choose US dollars:");
        int x = scanner.nextInt();
        if(rate>=15||rate<0){
            System.out.println("Exchange Rate Input Out of Normal Range");
            return;
        }
        if(x==0){
            System.out.print("Please enter US dollars:");
            double Dollar = scanner.nextDouble();
            double RMB = Dollar*6.81;
            System.out.println(Dollar+"The dollar is"+RMB+"element");
        }else if(x==1){
            System.out.print("Please enter RMB:");
            double RMB1 = scanner.nextDouble();
            double Dollar1 = RMB1/6.81;
            System.out.println(RMB1+"Yuan is"+Dollar1+"dollar");
        }else{
            System.out.println("Misselection");
        }
    }
}

The random(); method generates double random numbers greater than or equal to 0.0 and less than 1.0 (0.0 <=Math.random()<1.0)
That is, [0,1] left-closed and right-opened.

Topics: Java Programming less