我浏览了各种论坛和网站,试图找出我哪里出错了,但没有成功。
public void updatePlayerLabels()
{
if(currPlayer == 0)
lblP1Name.setText(lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore()));
else
lblP2Name.setText(lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore()));
}
错误似乎出现在我对两个语句调用 getScore() 方法的地方。我收到 2 条“此处不允许‘无效’类型”的消息。这是播放器类的片段。
public Player(int number, String name)
{
this.number = number;
this.name = name;
score = 0;
}
public int getScore()
{
return score;
}
据我所知,我不应该看到该错误,因为我在构造函数中将分数设置为 0,并且我在调用该方法之前构造了玩家。
此外,我在代码的其他地方使用 getScore() 方法也没有任何问题,我确信这是有问题的方法,因为当我从这两行中删除它时,错误就消失了。
最佳答案
您正在调用 lblP1Name.setText()
两次(第二次调用 lblP1Name.setText()
的“结果”)
应该是:
if(currPlayer == 0)
lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
else
lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
关于Java 错误消息 - 调用返回整数的方法时,此处不允许使用 'void' 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9054115/