Java 错误消息 - “void”调用返回整数的方法时,此处不允许输入类型
我浏览了各种论坛和网站,试图找出我出错的地方,但没有运气。
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 条“此处不允许使用‘void’类型”消息。这是玩家类的片段。
public Player(int number, String name)
{
this.number = number;
this.name = name;
score = 0;
}
public int getScore()
{
return score;
}
据我所知,我不应该看到这个错误,因为我在构造函数中将分数设置为 0,并且在调用该方法之前构造了玩家。
另外,我在代码的其他地方使用 getScore() 方法没有任何问题,我确信这是有问题的方法,因为当我从这两行中删除它时,错误消失了。
I've had a look throughout various forums and sites trying to find out where I'm going wrong but with no luck.
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()));
}
the error seems to appear where I call the getScore() methods on both statements. I get 2 "'void' type not allowed here" messages. Here is a snippet of the player class.
public Player(int number, String name)
{
this.number = number;
this.name = name;
score = 0;
}
public int getScore()
{
return score;
}
As far as I can tell I should not be seeing that error as I set score to 0 in the constructor and I construct the players before that method is called.
Also I use the getScore() method elsewhere in the code without any problems, I am sure this is the problematic method as when I remove it from those 2 lines the error disappears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在调用
lblP1Name.setText()
两次(第二次的“结果”是调用lblP1Name.setText()
),它应该是:
You are calling
lblP1Name.setText()
twice (the second time with the "result" of callinglblP1Name.setText()
)It should be:
您的代码显示
内部设置文本调用将返回传递给外部调用的 void。这会导致错误。
Your code reads
The inner set text call will return the void that is passed to the outer call. This causes the error.
在这一部分中:
方法
setText()
接收一个String
并返回void,您试图设置lblP1Name
的文本,但内部lblP1Name.setText
将返回 void。试试这个:
In this part:
The method
setText()
receives aString
and returns void, you 're trying to set the text oflblP1Name
but the innerlblP1Name.setText
will return void.Try this instead: