我想我在这里混淆了一些东西。在players.add(名字) 我的指示是...... “
添加名为addPlayer新方法。而应采取的String玩家名称作为 论点。使用字符串创建一个新的播放器,并把它添加到球员名单。“
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; //2
PhraseGameController Player = new PhraseGameController(); //3
public void addPlayer(String name) {
Player players= new Player(name);
players.add(name); //// <<< Error Is Here
}
public ArrayList<Player> getPlayers() {
return players;
} //4
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;
}
}
and my player class..
package edu.htc.java1.phrasegame.model;
public class Player {
public int currentScore;
public int highScore;
public String name;
public String getPlayerName() {
return "Me";
}
public Player(String getName) {
getName = "Me";
}
public int modifyScore(int points) {
currentScore = points + currentScore;
return currentScore;
}
public void finalizeScore() {
if ( currentScore >= highScore){
highScore = currentScore;
}
currentScore = 0;
}
// Getters and setters
public int getCurrentScore() {
return currentScore;
}
public int getHighScore() {
return highScore;
}
}
答案 0 :(得分:1)
试试这个:
public void addPlayer(String name) {
players.add(new Player(name)); // add the player object not name
}
答案 1 :(得分:0)
您需要添加Player
对象而不是String
名称,如:
public void addPlayer(String name) {
Player player = new Player(name);
players.add(player); // add the player object not name
}