我在 Eclipse 中收到错误消息:
The type of the expression must be an array type but it resolved to Player.
我创建了一个对象 Player。用户通过 JOptionPane 输入他们想要的玩家数量。我正在尝试将玩家姓名存储在数组中。
public class Project3 {
public static void main(String[] args){
String input = JOptionPane.showInputDialog("Enter the number of players: ");
int numPlayers = Integer.parseInt(input);
Player nameOfPlayers;
for(int i = 0; i < numPlayers; i++){
nameOfPlayers[i] = new Player(JOptionPane.showInputDialog("Enter the number of players: "));
if (input == null || input.equals(" ")) throw new IllegalArgumentException("Must enter valid name!!!");
}
}
这是我的类(class)玩家:
public class Player {
private String name;
public Player(String name){
if(name == null || name.equals(" "))
throw new IllegalArgumentException("Must enter a name. ");
this.name = name;
}
public void addWord(Word w){
}
public int getScore(){
}
}
最佳答案
您尚未创建数组。
也许您的意思是 Player [] nameOfPlayers = new Player[somevalue];
关于java - 需要帮助编写文字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119561/