我剛剛開始使用Java,我一直試圖讓控制檯輸入正常工作。下面的代碼:控制檯輸入等待它不應該
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
這應該只是需要輸入一次,並打印輸入,就像這樣:
Write a word:
DEBUG 1
Hello //My input
DEBUG 2
Hello //Output
但是,這是發生了什麼:
Write a word:
Hello //My input
DEBUG 1
//Waiting for new input
DEBUG 2
Hello //The first input
所以,似乎對我來說,它在某種程度上需要輸入在線掃描儀鍵盤=新的掃描儀(System.in);然後把它放在我的變量str。我用GCJ用下面的命令來編譯編譯和運行:
javac hello_world.java
java hello_world
編輯:我試圖運行在另一臺計算機上的程序現在,使用Ubuntu 10.04 64位,而不是Ubuntu的10.04 32位作爲之前。我以相同的方式運行它,並沒有重新編譯,程序工作正常。
任何想法,爲什麼它的行爲是這樣的?
你嘗試過使用調試器嗎? – home