Java困惑于如何安排"如果"声明

时间:2017-06-06 22:41:24

标签: java loops if-statement

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.

3 个答案:

答案 0 :(得分:1)

您实际上希望按以下顺序执行操作(假设您攻击):

  1. 询问玩家是否要攻击或逃离
  2. 你攻击
  3. 看你是否杀死了怪物;如果是这样,游戏结束了
  4. 怪物攻击
  5. 看看怪物是否杀了你;如果是这样,游戏结束了
  6. 重复直到#3或#5出现
  7. 你反向#3和#5。

    另外,以下一行:

    else if (enemies[1].getHP() <= 0)
    

    我认为这是一个错字。我假设你真的打算做

    else if (enemies[i].getHP() <= 0)
    

答案 1 :(得分:1)

试试这个:

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;
    }
}

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

答案 2 :(得分:0)

似乎不是使用else if,而是你想要的是这样的:

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

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

    // ...
}
// Then resolve the deaths immediately after.
if (user.getP_HP() <= 0) {
    // ...
    alive = false;
}
if (enemies[1].getHP() <= 0) {
    // ...
    alive = false;
}