The fourth day of learning java

java flow control

User Interaction Scanner

  • There is no interaction between programs and humans in the basic grammar learned earlier. But Java provides me with such a utility class, we can get the user's input. java.util.Scanner is a new feature of Java5, I can get user input through Scanner class

  • Basic syntax:

    Scanner s = new Scanner (Syetem.in);

    The input string is obtained through the next() and nextLine() methods of the Scanner class. Before reading, we generally need to use hasNext() and hasNextLine() to determine whether there is still input data.

next():

1. Be sure to read valid characters before ending the input.

2. The next() method will automatically remove the blanks encountered before valid characters are input.

3. Only after valid characters are entered, the blanks entered after them are used as separators or terminators.

4.next() can't get a string with spaces

5. Use the next method to receive:

package com.kong.scanner;

import java.util.Scanner;

public class Damo01 {
    public static void main(String[] args) {
        //Create a scan object to receive keyboard data
       Scanner scanner = new Scanner(System.in);

       System.out.println("use next way to receive:");
       //Determine if the user has entered a string
        if (scanner.hasNext()){
            //Receive using next method
            String str = scanner.next();
            System.out.println("The output is:"+str);
        }
        //Any class that belongs to the IO stream will always occupy resources if it is not closed. It is necessary to develop a good habit and close it after use.
        scanner.close();

    }
}

nextLin():

1. Use Enter as the end character, that is to say, the nextLine() method returns all characters before the input carriage return.

2. Blanks can be obtained.

3. Use the nextLine method to receive:

package com.kong.scanner;

import java.util.Scanner;

public class Damo02 {
    public static void main(String[] args) {
        //Receive data from keyboard
        Scanner scanner = new Scanner(System.in);

        System.out.println("use nextLine way to receive:");

        //Determine if there is still input
        if (scanner.hasNextLine()){
            //Receive using next method
            String str = scanner.nextLine();
            System.out.println("The output is:"+str);
        }
        scanner.close();
    }
}

4. Commonly used nextLine method to receive:

package com.kong.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //Receive data from keyboard
        Scanner scanner = new Scanner(System.in);

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

            String str = scanner.nextLine();

            System.out.println("The output is:"+str);

        scanner.close();
    }
}

Advanced use of Scanner

  • Integers and decimals
package com.kong.scanner;

import java.util.Scanner;

public class Demo04 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //Receive data from keyboard
        int i = 0;
        float f = 0.0f;

        System.out.println("Please enter an integer");

        //if. . . So
        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 a decimal");
        //if. . . So
        if (scanner.hasNextFloat()){
            f = scanner .nextFloat();
            System.out.println("Integer data:"+f);
        }else {
            System.out.println("The input is not integer data!");
        }
        scanner.close();
    }
}
  • Enter multiple numbers and find their sum and average
package com.kong.scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //We can input multiple numbers, and find the sum and average, confirm each input with a carriage return, end the input by inputting a non-number and output the execution result:

        Scanner scanner = new Scanner(System.in);
        //and
        double sum = 0;
        //Count how many numbers were entered
        int m =0;

        //Determine whether there is still input through the loop, and sum and count each time in it
        while (scanner.hasNextBigDecimal()){
            double x = scanner.nextDouble();
            m = m + 1;  //m++
            sum = sum + x;
            System.out.println("you entered the"+m+"data, then the current result sum="+sum);
        }

        System.out.println(m + "The sum of the numbers is" +sum);
        System.out.println(m + "The average of the numbers is" +(sum/m));

        scanner.close();
    }
}

structure

sequential structure

  • The basic structure of JAVA is the sequence structure, unless otherwise specified, execute sentence by sentence in sequence.
  • Sequential structures are the simplest algorithmic structures.
  • Statements and statements, boxes and boxes are carried out in order from top to bottom. It is composed of several processing steps that are executed in sequence. It is a basic algorithm structure that any algorithm cannot do without. .
package com.kong.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

choose structure

1. if single-choice structure

  • We often need to judge whether something is feasible before we execute it. Such a process is represented by the if syntax in the program
  • grammar:
if(boolean expression){
    //The statement that will be executed if the boolean expression is true
}
  • if single choice structure test example
package com.kong.struct;

import java.util.Scanner;

public class IFDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

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

        //equals: determine whether strings are equal
        if (s.equals("Hello")) {
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}

2. if double selection structure

  • Now there is a demand. The company wants to acquire a software. If it succeeds, it will pay people 1 million yuan. If it fails, it will find someone to develop it by itself. Such a requirement cannot be solved with an if. We need to have two judgments and a double-selection structure, so there is an if-else structure.
  • grammar:
if(boolean expression){
    //if the boolean expression evaluates to true
}else{
    //if the boolean expression evaluates to false
}
  • if double-choice structure test example
package com.kong.struct;

import java.util.Scanner;

public class IFDemo02 {
    public static void main(String[] args) {
        //A test score greater than 60 is a pass, and a score less than 60 is a fail.
        Scanner scanner = new Scanner(System.in);

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

        if (score>60){
            System.out.println("Pass");
        }else{
            System.out.println("failed");
        }
        scanner.close();
    }
}

3. if multiple choice structure

  • In life, we often have more than two choices.
  • grammar:
if(boolean expression 1){
    //Execute code if boolean expression 1 evaluates to true
}else if(boolean expression 2){
    //Execute code if boolean expression 2 evaluates to true
}else if(boolean expression 3{
    //Execute code if boolean expression 3 evaluates to true
}else{
    //Execute code if none of the above boolean expressions are true
}
  • if multiple choice structure test case
package com.kong.struct;

import java.util.Scanner;

public class IFDeom03 {
    public static void main(String[] args) {
        //A test score greater than 60 is a pass, and a score less than 60 is a fail.
        Scanner scanner = new Scanner(System.in);

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

        if (score==100){
            System.out.println("congratulations");
        }else if (score<100 && score>=90){
            System.out.println("A class");
        }else if (score<90 && score>=80){
            System.out.println("B class");
        }else if (score<80 && score>=70){
            System.out.println("C class");
        }else if (score<70 && score>=60) {
            System.out.println("D class");
        }else if (score<60 && score>=00) {
            System.out.println("Failed grades");
        }else{
            System.out.println("Illegal grades");
        }
        scanner.close();
    }

4. Nested if structure

  • It is legal to use nested if...else statements. That means you can use an if or else if statement inside another if or else if statement. You can nest else if ...else like if statements.
  • grammar:
if(boolean expression 1){
     //Execute code if boolean expression 1 evaluates to true
  if(boolean expression 2){
    //Execute code if boolean expression 2 evaluates to true
   }
}

5. switch multiple selection structure

  • Another way to implement multiple-choice constructs is the switch case statement.

  • A switch case statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.

  • Variable types in switch statements can be:

    1.byte, short, int or char

    2. Starting with Java SE 7

    3.switch supports string String type

    4. At the same time, the case label must be a string constant or literal

  • Example of switch letters:

package com.kong.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {

        char grade = 'A';

        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("failed");
                break;//optional
            case 'E':
                System.out.println("Score too low");
                break;//optional
            default:
                System.out.println("Unknown level");
        }
    }
}
  • Example of switch string:
package com.kong.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "Jincheng";
        //New in JDK7, expression results can be strings! ! !
        //Decompile java---class (bytecode file)---decompile (IDEA)

        switch (name){
            case "empty blue":
                System.out.println("empty blue");
                break;
            case "Jincheng":
                System.out.println("Jincheng");
                break;
            default:
                System.out.println("What are you doing!");
        }
    }
}

Tags: Java JavaEE

Posted by cbolson on Wed, 18 May 2022 01:22:25 +0300