mardi 6 juin 2017

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.

Aucun commentaire:

Enregistrer un commentaire