2016-04-23 78 views
-5

我正在練習如何使用IntelliJ Idea JAVA編輯器進行編碼。無法使用控制檯方法。我如何使用它?

我試圖使用控制檯方法示例:

System.console().printf("Hello World'); OR System.console().readLine("Good Morning);

,但由於某種原因,我不斷收到一個錯誤。 如何使用控制檯方法使其工作?

enter image description here

+0

如果粘貼錯誤這是更好而行,它指的不是一個屏幕截圖。幫助我們幫助你。具有例外的行有哪些代碼? – Acapulco

回答

1

不要使用System.console()這樣,而不是

System.out.println("Hello World"); 

System.out.printf("%s%n", "Hello World"); 

而且從System.in閱讀,我會建議一個Scanner;像

Scanner scan = new Scanner(System.in); 
System.out.println("Please enter a line: "); 
String line = scan.nextLine(); 
System.out.println("You entered: " + line); 

我之所以將避免System.console()在其中指出虛擬機是否有一個控制檯取決於底層平臺,也取決於在其中虛擬機被調用的方式的Javadoc記錄。

Console cons = System.console(); 
if (cons != null) { 
    String line = cons.readLine(); 
    System.out.println(line); 
} else { 
    System.out.println("no console"); 
} 

在我的IDE(如在你的),沒有System.console()

相關問題