Java process control 1 (Scanner sequence structure, selection structure)

Java process control

User interaction Scanner

  • We can get the user's input through the Scanner class
  • Basic syntax:
Scanner s = new Scanner(System.in)
  • Get the input string through the next() and nextLine() methods of Scanner class. Before reading, we generally need to use hasNext() and hasNextLine() to judge whether there is still input data.
hasNext
    public static void main(String[] args) {

        //Create a scanner object to accept keyboard data
        Scanner scanner = new Scanner(System.in);

        System.out.println("use next Method receiving:");//Program and waiting for user input

        //Judge whether the user has entered a string
        if (scanner.hasNext()){
            //Receive using the next method
            String str = scanner.next();
            System.out.println("The input content is:"+ str);
            //If there is a space in the input content, the content will be interrupted and only the content before the space will be output

        }

        scanner.close();//If the class of IO stream is not closed, it will occupy resources all the time. It is necessary to form a good habit to close it when it is used up


    }
hasNextLine()
    public static void main(String[] args) {
        //Accept data from keyboard
        Scanner scanner = new Scanner(System.in);//After creating the right side of the equal sign, quickly create the left side Ctrl+Alt+V

        System.out.println("use nextLine Method receiving:");

        //Determine whether there is any input
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("The output contents are:"+str);
            //Spaces will not be interrupted at this time
        }

        scanner.close();

    }
  • next()
    • Be sure to read valid characters before you can end the input
    • The next () method will automatically remove the blank space before entering valid characters
    • Only after entering a valid character will the blank space after it be used as the separator or terminator
    • next() cannot get a string with spaces
  • nextLine()
    • End with Enter, that is, the nextLine() method returns all characters before entering carriage return
    • Can get blank
   
            //Receive data from keyboard
        int i = 0;
        float f = 0.0f;

        System.out.println("please enter an integer:");

        //If that
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("Integer data:"+i);
        }else{
            System.out.println("The input is not integer data!");
        }

        System.out.println("Please enter decimal:");

        //If that
        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("Decimal data:"+f);
        }else{
            System.out.println("The input is not decimal data!");
        }

        
        scanner.close();

Sequential structure

  • The basic structure of JAVA is sequential structure. Unless otherwise specified, it will be executed sentence by sentence in order
  • Sequential structure is the simplest algorithm structure
  • Between statements and between boxes, it is carried out in the order from top to bottom. It is composed of several processing steps executed in turn. It is the basic algorithm structure that any algorithm is inseparable from

Select structure

  • if single selection structure
if(Boolean expression){
    //If the Boolean expression is true, the statement is executed
}

Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter:");
        String s = scanner.nextLine();

        //equals: determines whether the strings are equal
        if (s.equals("Hello")){
            System.out.println(s);
        }

        System.out.println("End");
        
        scanner.close();
  • if double selection structure
if(Boolean expression){
    //If the value of the Boolean expression is true
}else{
    //If the value of the Boolean expression is false
}

Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter your grade");
        int score = scanner.nextInt();

        if (score>60){
            System.out.println("pass");
        }else{
            System.out.println("fail,");
        }


        scanner.close();
  • if multiple selection structure
if(Boolean expression 1){
    //If the value of Boolean expression 1 is true, execute the code
}else if(Boolean expression 2){
    //If the value of Boolean expression 2 is true, execute the code
}else if(Boolean expression 3){
    //If the value of Boolean expression 3 is true, execute the code
}else {
    //If none of the above Boolean expressions execute true
}

 /*
        if The statement can have at most one else statement, which is after all else if statements
        if A statement can have several else if statements, which must precede the else statement
        Once one else if statement is detected as true, the other else if and else statements will skip execution
         */

        System.out.println("Please enter your grade:");
        int score = scanner.nextInt();

        if (score==100){
            System.out.println("Congratulations, full marks");
        }else if (score<100 && score>=90){
            System.out.println("A level");
        }else if (score<90 && score>=80){
            System.out.println("B level");
        }else if (score<80 && score>=70){
            System.out.println("C level");
        }else if (score<70 && score>=60){
            System.out.println("D level");
        }else if (score<60 && score>=0){
            System.out.println("fail,");
        } else {
            System.out.println("Illegal results");
        }


        scanner.close();
  • Nested if structure
if (Boolean expression 1){
    //If Boolean expression 1 is true, execute the code
    if(Boolean expression 2){
        //If Boolean expression 2 is true, execute the code
    }
}
  • switch multiple selection structure

  • Another implementation method of multi selection structure is switch case statement

  • The switch case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch

  • The variable type in the switch statement can be

    • byte, short, int or char
    • Start with Java SE7
    • switch supports String type
    • At the same time, the case tag must be a string constant or literal
switch(expression){
    case value:
        //sentence
        break;//Optional
    case value:
        //Optional
        break;//Optional
        //You can have any number of case statements
    default://Optional
        //sentence
}

//case penetration switch matches a specific value
        char grade = 'C';

        switch (grade){
            case 'A':
                System.out.println("excellent");
                break;//Optional
            case 'B':
                System.out.println("good");
                break;//Optional
            case 'C':
                System.out.println("pass");
                break;//Optional
            case 'D':
                System.out.println("make persistent efforts");
                break;//Optional
            case 'E':
                System.out.println("fail");
                break;//Optional
            default:
                System.out.println("Unknown level");
                break;//Optional

Posted by ryanlwh on Mon, 18 Apr 2022 06:32:36 +0930