我正在創建一個簡單的故事,它偶爾會提示用戶按ENTER鍵。它在我第一次提示時運行,但它會立即執行其他提示,也許是因爲程序運行速度非常快,直到您按下ENTER鍵時,它已經運行檢查提示。Java控制檯在繼續之前提示輸入ENTER
任何想法?代碼如下。
System.out.println("...*You wake up*...");
System.out.println("You are in class... you must have fallen asleep.");
System.out.println("But where is everybody?\n");
promptEnterKey();
System.out.println("You look around and see writing on the chalkboard that says CBT 162");
promptEnterKey();
//////////////////////////////////////////////////////
public void promptEnterKey(){
System.out.println("Press \"ENTER\" to continue...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
我想你需要使用掃描儀類 – Emrage 2014-10-03 18:16:34
看看[這個SO帖子](http://stackoverflow.com/questions/7877529/java-string-scanner-input-does-not-wait-for -info-moves-directly-to-next-stateme)...請在回收相同的東西之前閱讀其他問題 – starvator 2014-10-03 18:17:39
將來,您還可以'System.out.print(System.in.read());'看看發生了什麼。 [This Ascii table](http://www.asciitable.com/)指出正在讀取回車符和換行符。 – Compass 2014-10-03 18:24:06