Java 错误消息 - “void”调用返回整数的方法时,此处不允许输入类型

发布于 2024-12-29 18:34:35 字数 768 浏览 3 评论 0原文

我浏览了各种论坛和网站,试图找出我出错的地方,但没有运气。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2025-01-05 18:34:35

您正在调用 lblP1Name.setText() 两次(第二次的“结果”是调用 lblP1Name.setText()),

它应该是:

if(currPlayer == 0)
    lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
else
    lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());

You are calling lblP1Name.setText() twice (the second time with the "result" of calling lblP1Name.setText())

It should be:

if(currPlayer == 0)
    lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
else
    lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
陌路终见情 2025-01-05 18:34:35

您的代码显示

 lblP1Name.setText(lblP1Name.setText(.....));

内部设置文本调用将返回传递给外部调用的 void。这会导致错误。

Your code reads

 lblP1Name.setText(lblP1Name.setText(.....));

The inner set text call will return the void that is passed to the outer call. This causes the error.

寻梦旅人 2025-01-05 18:34:35

在这一部分中:

lblP1Name.setText(lblP1Name.setText(...

方法setText()接收一个String并返回void,您试图设置lblP1Name的文本,但内部lblP1Name.setText 将返回 void。

试试这个:

public void updatePlayerLabels() {
    if (currPlayer == 0)
        lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
    else
        lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
}

In this part:

lblP1Name.setText(lblP1Name.setText(...

The method setText() receives a String and returns void, you 're trying to set the text of lblP1Name but the inner lblP1Name.setText will return void.

Try this instead:

public void updatePlayerLabels() {
    if (currPlayer == 0)
        lblP1Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
    else
        lblP2Name.setText(myPlayers[currPlayer].getName() + " - " + myPlayers[currPlayer].getScore());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文