gpt4 book ai didi

java - Player 中的 setPlayerHp() 不能应用于整数

转载 作者:行者123 更新时间:2023-12-02 04:01:58 25 4
gpt4 key购买 nike

我正在为我的类(class)编写一个文本冒险应用程序,并且遇到了此错误。我有一个名为 Player 的类,具有三个关联变量(playerPos、playerHP 和playerInv[])

当我尝试从 Player 类获取或设置 playerHP 的值时,我得到以下信息:

“get/setPlayerHP 无法应用于 (int)”

我很困惑为什么。有什么建议吗?

    public class Player {

private int playerPos;
private int playerHP;
private String playerInv[];

Player(int startPos, int startHP, String[] newInventory[])
{
playerPos = startPos;
playerHP = startHP;
playerInv = newInventory[10];
}

public int getPlayerPos() {
return playerPos;
}

public void setPlayerPos(int playerPos) {
this.playerPos = playerPos;
}

public int getPlayerHP(){
return playerHP;
}

public void setPlayerHP(){
this.playerHP = playerPos;
}

public String setPlayerInv() {
return playerInv[10];
}

public void setPlayerInv(String inventory) {
for (int i=0; i<10; i++)
{
this.playerInv[i] = playerInv[i];
}
}
}

这是我尝试使用它的实例:

    public void PyrronQ1()
{
answerButton.setEnabled(true);
questionText.setText("What is my favourite colour?");
answerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if (answerText.getText().toString() == "Red")
{
PyrronQ2();
}
else
{
thePlayer.setPlayerHP(thePlayer.getPlayerHP() -20);
if (thePlayer.getPlayerHP() <= 0)
{
thePlayer.setPlayerPos(0);
setupControls();
RoomText.setText(thedungeon[thePlayer.getPlayerPos()].getName());
setupDirectionButtons();
thePlayer.setPlayerHP(100);


}
}
}

最佳答案

您定义了一个不带参数的方法:

public void setPlayerHP(){
this.playerHP = playerPos;
}

因此尝试将整数传递到此方法中将不会执行任何操作。

您可能想向此方法添加一个参数,例如:

public void setPlayerHP(int hp) {
// ...
}

关于java - Player 中的 setPlayerHp() 不能应用于整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34794763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com