使用控制台读取输入(仅在IDE外部可用):
System.out.print("Enter something:");String input = System.console().readLine();另一种方法(适用于所有地方):
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Test { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter String"); String s = br.readLine(); System.out.print("Enter Integer:"); try { int i = Integer.parseInt(br.readLine()); } catch(NumberFormatException nfe) { System.err.println("Invalid Format!"); } }}System.console()在IDE中返回null。因此,如果你真的需要使用
System.console()



