如何在Java中获取用户输入?


如何在Java中获取用户输入?

最简单的方法之一是使用Scanner如下对象:

import java.util.Scanner;

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int.
//once finished
reader.close();