Eclipse拒绝我的public void init方法 [英] Eclipse is rejecting my public void init method

查看:814
本文介绍了Eclipse拒绝我的public void init方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse SDK v3.2.1拒绝了我的 public void init 方法。

我使用以下导入:

Eclipse SDK v3.2.1 is rejecting my public void init method.
I'm using the following imports:

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

我的程序有一个run()方法和一个init这些错误

My program has a run() method and an init() method but the init() is causing these errors

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

请注意,这不是一个独立的应用程序。只需从Eclipse编辑器运行它。

Note, this is not a stand-alone application yet. Just running it from the Eclipse editor.

显然在acm.program库中有一个init方法。如何实现我自己的初始化方法,而不试图覆盖acm.program内置的?我试着让我的初始化方法私人与私人的无效初始化但然后我得到:

Apparently there is a an init method in the acm.program library. How do I implement my own initization method without attempting to override the acm.program built-in one? I've tried making my init method private with private void init but then I get:

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

这里是代码到目前为止。错误是与init()。

Here is the code so far. The error is with the 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() {


b $ b

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

/* When the user plays Hangman, the computer first selects a secret word at random from a list built into the program. The program then prints out a row of dashes—one for each letter in the secret word—and asks the user to guess a letter. If the user guesses a letter that is in the word, the word is redisplayed with all instances of that letter shown in the correct positions, along with any letters correctly guessed on previous turns. If the letter does not appear in the word, the user is charged with an incorrect guess. The user keeps guessing letters until either (1) the user has correctly guessed all the letters in the word or (2) the user has made eight incorrect guesses. */

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

//在状态字中建立破折号秘密词

// build the dashes in status word as long as the secret word.

        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.
    }
}

//生成和/ -----显示

//Build and/or update the dash word ------ displayed

    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


推荐答案

我想您正在使用斯坦福CS106A课程,这是导致init最后的问题。问题是 Karel.jar 库必须在前几个课程中导入。我想这个库有init()方法作为final,这是根本原因。所以简单的解决方案是从参考库中删除Karel.jar:

I suppose you are taking Stanford CS106A course and that is causing the init final issue. The problem is the Karel.jar library u must have imported during the first few lectures. I suppose that library has init() method as final and that is the root cause. So the simple solution is to remove Karel.jar from the reference library as :


  1. 在软件包资源管理器中选择参考库
  2. 选择构建路径。上下文菜单。

  3. 选择从构建路径中移除

  1. In package explorer choose Reference Libraries.
  2. Right click on Karel.jar.
  3. Chose Build Path from the context menu.
  4. Chose Remove from Build Path.

但是,如果你需要使用Eclipse再次使用Karel编程,你将不得不通过类似的课程,但选择导入库,从Assignment 1包中再次导入参考库。
为确保您继续使用acm方法,请确保从 jtf.acm.org 。要添加库,请使用 Eclipse文档或只是Google。

But in case u need to do Karel Programming again using Eclipse you'll have to import the Reference Library again from the Assignment 1 Package by follwing a similar course but choosing to import the library. To make sure you keep on using the acm methods, please make sure to import acm.jar by downloading it from jtf.acm.org. To add libraries you make use Eclipse Documentation or just Google it.

这篇关于Eclipse拒绝我的public void init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆