2013-07-17 121 views
0

所以我試圖做一個遊戲,用戶輸入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); 
    } 
} 

} 

如果有人知道如何防止這個錯誤,那麼我會很感激。如果您有另一種清除控制檯的方法,那很好,但我想知道爲什麼這種方式無法正常工作。

謝謝

+0

[Java:Clear the console]的可能的重複(http://stackoverflow.com/questions/2979383/java-clear-the-console) – assylias

+0

'Runtime.getRuntime().exec(「clear」 );'試圖運行一個名爲'clear'的程序 - 它找不到它並給你一個例外。 – assylias

回答

0

在控制檯相關的API中幾乎沒有任何東西可以做清晰的屏幕。但是,我認爲你可以通過println()獲得同樣的效果。許多膩子客戶端清理頁面,然後向上滾動。

private static final int PAGE_SIZE = 25; 

public static void main(String[] args) { 
      // ... 
    clearScreen(); 
} 

private static void clearScreen() { 
    for (int i = 0; i < PAGE_SIZE; i++) { 
     System.out.println(); 
    } 
} 
+0

是的,我試過CLS&清除,既不工作......?我在原始文章 – Sam

+2

中沒有說清楚這很正常:那些是'cmd'的內置命令,而不是你可以調用的可執行文件。 – fge

+0

@JackSmith對不起,我忽略了這一點。這種新方法能爲你工作嗎? –

0
final String ANSI_CLS = "\u001b[2J"; 
    final String ANSI_HOME = "\u001b[H"; 
    System.out.print(ANSI_CLS + ANSI_HOME); 
    System.out.flush();  

試試這個。

這對我很好。 (Kubuntu)