Eclipse拒绝我的public void init方法

时间:2012-02-17 16:13:59

标签: java eclipse compiler-errors

Eclipse SDK v3.2.1拒绝了我的 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的情况下实现自己的初始化方法?我已经尝试使用 private void 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() {

/ *当用户玩Hangman时,计算机首先选择一个密码     从程序内置的列表中随机选择。程序然后打印出一行破折号   对于秘密词中的每个字母 - 并要求用户猜一个字母。如果用户猜对了   在单词中的一个字母,该单词将重新显示该字母的所有实例   显示在正确的位置,以及在之前的转弯中正确猜到的任何字母。   如果单词中没有出现该字母,则向用户收取不正确的猜测。   用户一直猜测字母,直到(1)用户正确猜到了所有字母   单词中的字母或(2)用户做了八次不正确的猜测。 * /

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

1 个答案:

答案 0 :(得分:0)

我想你正在服用Stanford CS106A course,这导致了init最终问题。问题是你必须在前几个讲座中导入的 Karel.jar 库。我想这个库有init()方法作为final,这是根本原因。因此,简单的解决方案是从参考库中删除Karel.jar:

  1. 在软件包资源管理器中,选择参考库
  2. 右键点击 Karel.jar
  3. 从上下文菜单中选择构建路径
  4. 选择从构建路径中删除
  5. 但是如果你需要再次使用Eclipse进行Karel编程,你必须再次从Assignment 1 Package中导入参考库,方法是选择导入库。 为了确保继续使用acm方法,请确保从jtf.acm.org下载acm.jar。要添加库,您可以使用Eclipse Documentation或只使用Google。