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

前端 未结 15 1976
我寻月下人不归
我寻月下人不归 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 06:50

    A simple example:

    import java.util.Scanner;
    
    public class Example
    {
        public static void main(String[] args)
        {
            int number1, number2, sum;
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter First multiple");
            number1 = input.nextInt();
    
            System.out.println("Enter second multiple");
            number2 = input.nextInt();
    
            sum = number1 * number2;
    
            System.out.printf("The product of both number is %d", sum);
        }
    }
    

提交回复
热议问题