How can I read input from the console using the Scanner class in Java?

前端 未结 15 2393
我寻月下人不归
我寻月下人不归 2020-11-21 06:19

How could I read input from the console using the Scanner class? Something like this:

System.out.println(\"Enter your username: \");
Scanner = i         


        
15条回答
  •  别那么骄傲
    2020-11-21 07:10

    import java.util.Scanner;
    
    public class ScannerDemo {
        public static void main(String[] arguments){
            Scanner input = new Scanner(System.in);
    
            String username;
            double age;
            String gender;
            String marital_status;
            int telephone_number;
    
            // Allows a person to enter his/her name   
            Scanner one = new Scanner(System.in);
            System.out.println("Enter Name:" );  
            username = one.next();
            System.out.println("Name accepted " + username);
    
            // Allows a person to enter his/her age   
            Scanner two = new Scanner(System.in);
            System.out.println("Enter Age:" );  
            age = two.nextDouble();
            System.out.println("Age accepted " + age);
    
            // Allows a person to enter his/her gender  
            Scanner three = new Scanner(System.in);
            System.out.println("Enter Gender:" );  
            gender = three.next();
            System.out.println("Gender accepted " + gender);
    
            // Allows a person to enter his/her marital status
            Scanner four = new Scanner(System.in);
            System.out.println("Enter Marital status:" );  
            marital_status = four.next();
            System.out.println("Marital status accepted " + marital_status);
    
            // Allows a person to enter his/her telephone number
            Scanner five = new Scanner(System.in);
            System.out.println("Enter Telephone number:" );  
            telephone_number = five.nextInt();
            System.out.println("Telephone number accepted " + telephone_number);
        }
    }
    

提交回复
热议问题