2012-02-17 25 views
0

Eclipse SDK v3.2.1拒絕我的public void init方法。
我使用以下進口:Eclipse正在拒絕我的public void init方法

import acm.graphics.*; 
import acm.program.*; 
import acm.util.*; 

我的程序有一個run()方法和init()方法,但在init()是造成這些錯誤

- overrides acm.program.Program.init 
- Cannot override the final method from Program 

注意,這不是一個獨立的應用程序呢。只需從Eclipse編輯器運行它。

顯然在acm.program庫中有一個init方法。如何實現我自己的初始化方法而不嘗試覆蓋acm.program內置的方法?我試着讓我的init()方法的私人與私人無效的init但後來我得到:

- Cannot reduce the visibility of the inherited method from Program 
- Cannot override the final method from Program 

這裏是到目前爲止的代碼。該錯誤與init()。

public class Hangman extends GraphicsProgram { 

    //CONSTANTS 
private static int NUMBER_OF_INCORRECT_GUESSES = 8; 


//Initialize the program 
public void init() { //this gives compiler a problem 
HangmanCanvas canvas = new HangmanCanvas(); 
add(canvas); 
} 



public void run() { 

/*當用戶播放劊子手,計算機首先選擇在 隨機從內置到程序列表的密語。然後該程序打印出一行破折號 - 祕密字中每個字母的一個 - 並要求用戶猜測一個字母。如果用戶猜測 字詞中的一個字母,該字就會重新顯示,該字母顯示在正確位置的所有實例 以及正確猜測前一個字母的字母。 如果該字母沒有出現在單詞中,則用戶被判爲不正確的猜測。 用戶不斷猜測字母,直到(1)用戶已經正確猜出了單詞中所有的 字母或(2)用戶做出了8次錯誤的猜測。 */

HangmanLexicon lexicon = new HangmanLexicon(); 
RandomGenerator rgen = new RandomGenerator(); 
int wordIndex = rgen.nextInt(0, lexicon.getWordCount()-1); 

    while (true) { //allows multi-play 
     int play = 0; 
     String answer = readLine ("Want to play?"); 
     if(answer.equals("Y") || answer.equals("y") || answer.equals("yes") || answer.equals("Yes")) {play = 1;} 
     if(play == 0) {break;} 
     //Initialize some stuff 
     //get new random word 
     secretWord = lexicon.getWord(rgen.nextInt(0,wordIndex)); 
     println("Welcome to Hangman."); 
     secretWord = secretWord.toUpperCase(); // convert secret word to upper case 
     int length = secretWord.length(); 
     //reset game variables 
     String guess = ""; 
     //clear the canvas 
     canvas.reset(); 
     //reset the wrong answer count 
     wrong = 0; 

//建立空白狀態字

 currentWord = ""; //reset the word for multi-play 

//只要加密詞建立狀態字中的破折號。

 for (int i = 0; i < length; i++) { 
      currentWord += "-"; 
     } 
     println("The word looks like this " + currentWord); 

     while (wrong<NUMBER_OF_INCORRECT_GUESSES && !currentWord.equals(secretWord)) { 
      guess = "."; 
      char g = guess.charAt(0); 
      while (!Character.isLetter(g)){ //if input is not a character, keep asking 
       guess = readLine("Guess a letter: "); 
       guess = guess.toUpperCase(); 
       g = guess.charAt(0); 
       if (!Character.isLetter(g)){println("Your guess is not a single letter. Guess again: ");} 
      } 
      if(secretWord.indexOf(guess) < 0) {/*if guess is not in the secret word, increment wrong answer count and print message 
      to that effect. */ 
       wrong++; 
       println("Threre are no " + guess + "\'s in the word."); 
       println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong) + " guesses left."); 
      } 
      else { 
       println("That guess is correct."); 
       currentWord = wordBuild(guess); 
       if (currentWord.equals(secretWord)) { //if win print win but don't bother with the update to display 
        println("You win! You guessed the word: " + secretWord);} 

        else { println("The word now looks like this " + currentWord); //print the updated dash word 
        println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong) + " guesses left."); 
        }   
      } 
     } 

    if(!currentWord.equals(secretWord)) {println("You lose.");} //out of guesses and word is not good. 
    } 
} 

//建立和/或更新破折號字------顯示

public String wordBuild(String guess) { 
     String dashWord = ""; 
     for (int i = 0; i < secretWord.length(); i++) { 
       if(secretWord.charAt(i) == guess.charAt(0)) { 
       dashWord = dashWord + guess; 
       } 
        else {dashWord = dashWord + currentWord.charAt(i); 
       } 
     } 
      return dashWord; 

    } 



//INSTANCE VARS 

int wrong = 0; 
String currentWord = ""; 
String secretWord = "";  
private HangmanCanvas canvas; 

} //end of class 
+0

'final'方法不能被覆蓋。嘗試添加一個構造函數。 – 2012-02-17 16:16:52

+0

顯示你的'init'方法!另外,確保你有一個不同於'acm'的包。 – adarshr 2012-02-17 16:17:04

+0

發佈更多與問題相關的代碼。 – kosa 2012-02-17 16:17:30

回答

0

我想你正在服用Stanford CS106A course,並且導致初始化最後一個問題。問題是你在前幾個講座期間必須導入的圖書館Karel.jar。我想這個庫有init()方法作爲final,這是根本原因。所以,簡單的解決方案是從參考庫中刪除Karel.jar爲:

  1. 在Package Explorer中選擇資料室
  2. 右鍵點擊Karel.jar
  3. 從上下文菜單中選擇構建路徑
  4. 選擇從Build Path中刪除

但是,如果您需要再次使用Eclipse進行Karel編程,則必須通過類似課程再次從Assignment 1包中導入Reference Library,但選擇導入該庫。 爲確保您繼續使用acm方法,請確保通過從jtf.acm.org下載acm.jar進行導入。要添加庫,您可以使用Eclipse Documentation或只是谷歌它。

相關問題