所以我試圖做一個遊戲,用戶輸入5個單詞,然後控制檯被清除,然後他的對手然後必須猜測5話。我沒有做過任何類似於刪除空格或轉換爲一致的情況,因爲這只是我試圖學習,我已經知道如何做到這一點。儘管在系統嘗試(並且失敗)清除控制檯時得到這麼長的錯誤列表,該程序仍可以很好地執行。下面是輸出(注意:我也用「CLS」以及「明確」的,兩者不工作):Java清除控制檯錯誤:java.io.IOException:無法運行程序「清除」
| Player One Enter Words |
Word 1: why
Word 2: does
Word 3: this
Word 4: not
Word 5: work
java.io.IOException: Cannot run program "clear": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at OrigClass.main(OrigClass.java:18)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
| Player Two Guess Words |
Guess 1: not
Guess 2: work
Guess 3: why
Guess 4: does
Guess 5: this
Congrats. You Got A Perfect Score Of 5
這是我的實際代碼:
import java.util.Arrays;
import java.util.Scanner;
class OrigClass{
public static void main(String[] args){
Scanner scanObj = new Scanner(System.in);
String[] words;
words = new String[5];
String[] guess;
guess = new String[5];
System.out.println(" | Player One Enter Words | ");
for(int count = 1; count <= 5; count++){
System.out.print("Word " + count + ": ");
words[count-1] = scanObj.nextLine();
}
try{
Runtime.getRuntime().exec("clear");
}
catch (Exception e){
e.printStackTrace();
}
System.out.println(" | Player Two Guess Words | ");
for(int count2 = 1; count2 <= 5; count2++){
System.out.print("Guess " + count2 + ": ");
guess[count2-1] = scanObj.nextLine();
}
int score = 0;
for(int count3 = 0; count3 < 5; count3++){
if(Arrays.asList(words).contains(guess[count3])){
score++;
}
}
if(score >= 0 && score <= 2){
System.out.println("Unlucky. You Scored " + score);
}
else if (score > 2 && score < 5){
System.out.println("Good Effort. You Scored " + score);
}
else {
System.out.println("Congrats. You Got A Perfect Score Of " + score);
}
}
}
如果有人知道如何防止這個錯誤,那麼我會很感激。如果您有另一種清除控制檯的方法,那很好,但我想知道爲什麼這種方式無法正常工作。
謝謝
[Java:Clear the console]的可能的重複(http://stackoverflow.com/questions/2979383/java-clear-the-console) – assylias
'Runtime.getRuntime().exec(「clear」 );'試圖運行一個名爲'clear'的程序 - 它找不到它並給你一個例外。 – assylias