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个小时无法解决此问题。我想做的就是让敌人在其HP达到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 - Java对如何安排“if”语句感到困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44400805/

10-10 06:04