我想我几乎拥有它。
我的PhraseGameController类没有找到“isHidden”
我的错误消息是“isHidden无法解析为变量” “字母无法解析为变量” “对于PhraseGameController类型,方法isHidden()未定义 (我会评论我的问题区域)
●浏览currentPhrase中的每个字母
○检查是否隐藏
○如果是,则将isWon设置为false。使用break退出循环。
●返回isWon
package edu.htc.java1.phrasegame;
import edu.htc.java1.phrasegame.model.*;
import java.util.ArrayList;
public class PhraseGameController {
private Phrase currentPhrase;
private ArrayList<Character> guessed;
private ArrayList<Player> players;
boolean isRoundWon;Letter letter;
PhraseGameController Player = new PhraseGameController();
public void addPlayer(String name) {
Player player= new Player(name);
players.add(player);
}
public boolean isRoundWon(boolean isWon) {
isWon = true;
letter=new Letter();
if(letter.isHidden() = true) { ///<<<< Problem area, isHidden
isWon = false;
break;
} else if { (letter.isHidden() = false); ///<<<< Problem area, isHidden
isWon = true;
return isWon;
break;
}
public ArrayList<Player> getPlayers() {
return players;
}
public boolean doPlayerGuess(Character play) {
Character guess = Character.toUpperCase(play);
if(guessed.contains(play)){
throw new IllegalStateException("the letter was already guessed");
} else if (!String.valueOf(play).matches("[A-Z]")) {
throw new IllegalStateException("the guess should be a letter from AZ");
}
guessed.add(guess);
return currentPhrase.guessLetter(guess);
}
public PhraseGameController(){
currentPhrase = new Phrase("This is only a test");
guessed = new ArrayList<Character>();
}
public Phrase getCurrentPhrase() {
return currentPhrase;
}
public void setCurrentPhrase(Phrase currentPhrase) {
this.currentPhrase = currentPhrase;
}
public void setGuessedLetters(ArrayList<Character> guessed) {
this.guessed = guessed;
}
public ArrayList<Character> getGuessedLetters() {
return guessed;
}
}
和
package edu.htc.java1.phrasegame.model;
public class Letter {
public char letter;
public boolean isHidden;
public char getLetter() {
return letter;
}
public boolean isHidden() {
return isHidden;
}
public void unhide() {
isHidden = false;
}
public Letter(char letter) {
this.letter = letter;
if (String.valueOf(letter).matches("[A-Z]")) {
isHidden = true;
}
}
}
答案 0 :(得分:0)
您访问的方法错误。
isHidden.letter
应为letter.isHidden()
也不要同时推杆= true
。首先,无论如何都要对布尔表达式进行求值,这样你就不需要将它与任何东西进行比较。其次,您应该使用==或.equals()来比较两个值,=严格来说是分配
答案 1 :(得分:0)
IsHidden未定义为变量。这是一种方法。你应该把它称为.isHidden()。
答案 2 :(得分:0)
这样做的信函=新信();之后的那封letter.isHidden()
package edu.htc.java1.phrasegame;
import edu.htc.java1.phrasegame.model.*;
import java.util.ArrayList;
public class PhraseGameController {
private Phrase currentPhrase;
private ArrayList<Character> guessed;
private ArrayList<Player> players;
boolean isRoundWon;Letter letter;
PhraseGameController Player = new PhraseGameController();
public void addPlayer(String name) {
Player player= new Player(name);
players.add(player);
}
public boolean isRoundWon(boolean isWon) {
isWon = true;letter=new Letter();
if(letter.isHidden() = true) { ///<<<< Problem area, isHidden
isWon = false;
break;
} else if { (letter.isHidden() = false) ///<<<< Problem area, isHidden
isWon = true;
return isWon;
break;
}
public ArrayList<Player> getPlayers() {
return players;
}