Ramin Amiri
Ramin Amiri

Reputation: 161

Java confused on how to arrange "if" statements

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

Hello, I'm having problems solving this issue for about 2 hours now. What I want to do is for the enemy to die immediately after its HP reaches 0 or below, and same for the player.

This works only if the player kills the goblin first, and not vice versa.

Whenever the enemy kills the player first, it does not output that you have been killed. Instead, it gives you the option to choose if you want to attack or flee again. After choosing the attack option, it only then states that you have been killed. That's not all though, after that the goblin attacks you again before the program stops.

Here is the sample output if the enemy kills you first:

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.

Upvotes: 1

Views: 103

Answers (3)

KeOt777
KeOt777

Reputation: 247

try this:

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

you need to check if you're alive BEFORE asking for an input. Might not be the best solution, but it's a fast one and I hope it points you in the right direction

Upvotes: 1

Radiodef
Radiodef

Reputation: 37845

It seems like instead of using else if, what you want is something like this:

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

Upvotes: 0

You actually want to do things in the following order (assuming that you attack):

  1. Ask the player whether they want to attack or flee
  2. You attack
  3. See if you killed the monster; if so, the game is over
  4. The monster attacks
  5. See if the monster killed you; if so, the game is over
  6. Repeat until either #3 or #5 occurs

You reverse #3 and #5.

Also, the following line:

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

has what I assume is a typo. I assume that you actually meant to do

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

Upvotes: 1

Related Questions