Eclipse 拒绝我的 public void init 方法
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
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.*;
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
Note, this is not a stand-alone application yet. Just running it from the Eclipse editor.
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
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() {
/* 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;
// build the blank status word
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您正在学习斯坦福 CS106A 课程 这导致了 init 最终问题。问题是您必须在前几节课中导入的 Karel.jar 库。我认为该库将 init() 方法作为最终方法,这是根本原因。因此,简单的解决方案是从参考库中删除 Karel.jar,如下所示:
但如果您需要使用 Eclipse 再次进行卡雷尔编程,则必须通过遵循类似的课程但选择导入库,从作业 1 包中再次导入参考库。
为了确保您继续使用 acm 方法,请确保通过从 jtf 下载 acm.jar 来导入它。 acm.org。要添加库,请使用 Eclipse 文档 或 Google 即可。
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 :
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.