当前位置:网站首页>User interaction scanner usage Advanced Edition example

User interaction scanner usage Advanced Edition example

2022-06-25 23:23:00 Seasonal white September

User interaction scanner

java.util.Scanner yes java5 New features , Can pass Scanner Class to get user input

Basic grammar :

Scanner s=new Scanner(System.in);

adopt Scanner Class next() And nextLine() Method to get the input string , We usually need to Use hasNext() And hasNextLine() Determine whether there is any input data .

  • next() and hasNext() Method

    1. You must read valid characters before you can end the input

    2. Whitespace encountered before entering valid characters for ,next() Method will automatically remove it

      ** Such as Input hello world The output is hello **

    3. Only after entering a valid character can the blank space entered after it be used as a separator or Terminator

    4. next() Cannot get string with space

package com.zhang.scanner;

import java.util.Scanner;

public class Demo01 {
    
    public static void main(String[] args) {
    
        // Create a scanner object , For receiving keyboard data 
        Scanner scanner=new Scanner(System.in);
        System.out.println(" Use next Mode reception :");

        // Determine whether the user has input string 
        if (scanner.hasNext()){
    
            // Use next Mode to receive 
            String str=scanner.next();// The program will wait for the user to input 
            System.out.println(" The output is :"+str);
        }
        // All belong to IO If the class of the stream is not closed all the time   Will always take up resources , To form a good habit, turn it off after use 
        scanner.close();
    }
}

Use next Mode reception :
hello world
The output is :hello

  • nextLine() and hasNextLine()
    1. With Enter End of character in other words nextLine() Method returns all the characters before enter
    2. Can get blank
package com.zhang.scanner;

import java.util.Scanner;

public class Demo02 {
    
    public static void main(String[] args) {
    
        // Receive data from keyboard 
        Scanner scanner=new Scanner(System.in);
        System.out.println(" Use nextLine Mode reception :");
        // Judge whether there is any input 
        if (scanner.hasNextLine()){
    
            String str=scanner.nextLine();
            System.out.println(" The output is :"+str);
        }
        scanner.close();
    }
}

Use nextLine Mode reception :
hello world
The output is :hello world

package com.zhang.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 input data :");

        String str=scanner.nextLine();

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

        scanner.close();
    }
}

Please input data :
I love you! China
The output is : I love you! China

expand

package com.zhang.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;
        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 the decimal :");
        if (scanner.hasNextFloat()){
    
            f=scanner.nextFloat();
            System.out.println(" Fractional data :"+f);
        }else {
    
            System.out.println(" Input is not decimal data !");
        }

        scanner.close();
    }
}

please enter an integer :
10.1
The input is not integer data !
Please enter the decimal :
Fractional data :10.1

We can enter multiple numbers , And find the sum and the mean , Press enter to confirm each number entered , End the input and output the execution result by inputting non number

package com.zhang.scanner;

import java.util.Scanner;

public class Demo05 {
    
    public static void main(String[] args) {
    
        //  We can enter multiple numbers , And find the sum and the mean , Press enter to confirm each number entered , End the input and output the execution result by inputting non number 
        Scanner scanner=new Scanner(System.in);
        // and 
        double sum=0;
        // Calculate how many numbers are entered 
        int m=0;
        // Loop to see if there is any input , And sum up each time in it 
        while (scanner.hasNextDouble()){
    
            double x=scanner.nextDouble();
            m=m+1;//m++
            sum=sum+x;
            System.out.println(" You entered the number one "+m+" Data , The current result sum="+sum);
        }
        System.out.println(m+" The sum of the numbers is "+sum);
        System.out.println(m+" The average number is "+(sum/m));

        scanner.close();
    }
}

10
You entered the number one 1 Data , The current result sum=10.0
20
You entered the number one 2 Data , The current result sum=30.0
30
You entered the number one 3 Data , The current result sum=60.0
guyguy
3 The sum of the numbers is 60.0
3 The average number is 20.0

原网站

版权声明
本文为[Seasonal white September]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180604173131.html