gpt4 book ai didi

Java对如何排列 "if"语句感到困惑

转载 作者:行者123 更新时间:2023-11-30 02:30:34 26 4
gpt4 key购买 nike

int i = 1;
int answer;
int d;
int e;
boolean alive = true;

System.out.println("\n");

System.out.println(enemies[i].getName() + " has appeared!");

System.out.print("\n");

while (alive == true) {

System.out.println("Hitpoints[HP]: " + enemies[i].getHP());
System.out.println("Attack[ATK] : " + enemies[i].getATK());
System.out.println("Mana[MANA] : " + enemies[i].getMANA());

System.out.println("\n");

System.out.println("[1 - Attack]");
System.out.println("[2 - Flee]");

answer = In.getInt();

System.out.println("\n");

if (answer == 1) {

if (user.getP_HP() > 0) {
d = enemies[i].getHP() - user.getP_ATK();
enemies[i].setHP(d);

System.out.println(user.getP_Name() + " strikes the " + enemies[i].getName() + " for " + user.getP_ATK() + " damage.");
System.out.println(enemies[i].getName() + " has " + enemies[i].getHP() + " HP remaining.");
System.out.println("\n");
} else if (user.getP_HP() <= 0) {
System.out.println(user.getP_Name() + " has been slain by the " + enemies[i].getName() + "!");
alive = false;
}

if (enemies[i].getHP() > 0) {
e = user.getP_HP() - enemies[i].getATK();
user.setP_HP(e);

System.out.println("The " + enemies[i].getName() + " attacked you for " + enemies[i].getATK() + " damage.");
System.out.println(user.getP_Name() + " has " + user.getP_HP() + " HP remaining.");
System.out.println("\n");
} else if (enemies[1].getHP() <= 0) {
System.out.println(user.getP_Name() + " has slain the " + enemies[i].getName() + "!");
alive = false;
}

} else if (answer == 2) {
System.out.println("You try to run, but fall into a pit of spikes and die!");
alive = false;
}
}

您好,我在解决这个问题上遇到了大约 2 个小时的问题。我想做的是敌人的生命值达到0或以下后立即死亡,玩家也一样。

只有当玩家先杀死妖精时,这才有效,反之亦然。

每当敌人先杀死玩家时,它不会输出你已被杀死。相反,它让你可以选择是否要再次攻击或逃跑。选择攻击选项后,它才会表明你已被杀死。但这还不是全部,之后妖精会在程序停止之前再次攻击你。

这是敌人先杀死你时的示例输出:

stackoverflow strikes the Goblin for 1 damage.
Goblin has 12 HP remaining.


The Goblin attacked you for 3 damage.
stackoverflow has 1 HP remaining.


Hitpoints[HP]: 12
Attack[ATK] : 3
Mana[MANA] : 1


[1 - Attack]
[2 - Flee]
1


stackoverflow strikes the Goblin for 1 damage.
Goblin has 11 HP remaining.


The Goblin attacked you for 3 damage.
stackoverflow has -2 HP remaining.


Hitpoints[HP]: 11
Attack[ATK] : 3
Mana[MANA] : 1


[1 - Attack]
[2 - Flee]
1


stackoverflow has been slain by the Goblin!
The Goblin attacked you for 3 damage.
stackoverflow has -5 HP remaining.

最佳答案

试试这个:

int i = 1;
int answer;
int d;
int e;
boolean alive = true;

System.out.println("\n");

System.out.println(enemies[i].getName() + " has appeared!");

System.out.print("\n");

while (alive == true) {

System.out.println("Hitpoints[HP]: " + enemies[i].getHP());
System.out.println("Attack[ATK] : " + enemies[i].getATK());
System.out.println("Mana[MANA] : " + enemies[i].getMANA());

System.out.println("\n");
if (user.getP_HP() > 0) {
System.out.println("[1 - Attack]");
System.out.println("[2 - Flee]");

answer = In.getInt();

System.out.println("\n");
} else if (user.getP_HP() <= 0) {
System.out.println(user.getP_Name() + " has been slain by the " + enemies[i].getName() + "!");
alive = false;
answer = 0; //so it won't execute the rest of the code
}

if (answer == 1) {

d = enemies[i].getHP() - user.getP_ATK();
enemies[i].setHP(d);

System.out.println(user.getP_Name() + " strikes the " + enemies[i].getName() + " for " + user.getP_ATK() + " damage.");
System.out.println(enemies[i].getName() + " has " + enemies[i].getHP() + " HP remaining.");
System.out.println("\n");

if (enemies[i].getHP() > 0) {
e = user.getP_HP() - enemies[i].getATK();
user.setP_HP(e);

System.out.println("The " + enemies[i].getName() + " attacked you for " + enemies[i].getATK() + " damage.");
System.out.println(user.getP_Name() + " has " + user.getP_HP() + " HP remaining.");
System.out.println("\n");
} else if (enemies[1].getHP() <= 0) {
System.out.println(user.getP_Name() + " has slain the " + enemies[i].getName() + "!");
alive = false;
}

} else if (answer == 2) {
System.out.println("You try to run, but fall into a pit of spikes and die!");
alive = false;
}
}

在请求输入之前,您需要检查自己是否还活着。可能不是最好的解决方案,但它是一个快速的解决方案,我希望它能为您指明正确的方向

关于Java对如何排列 "if"语句感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400805/

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