jeudi 19 mars 2020

I keep on getting the else with out if error please help... the error is on the last" else if" statement

  • I keep getting the error else without if on the last else if statement, I am trying to fix it but I can't figure it out. The code is the system for inputs the player could type 1 does this 2 does this and for some reason 3 is not working correctly. I am new to Java as well and its my first project. I am just trying to make a text rpg.

         String input = in.nextLine();  
                if(input.equals("1")) {
                    int damageDealt = rand.nextInt(attackDamage) + playerLevel*2;
                    int damageTaken = rand.nextInt(enemyAttackDamage);
    
                    enemyHealth -=damageDealt;
                    playerHealth -= damageTaken;
    
                    System.out.println("\t> You whack the " + enemy + " for " + damageDealt  
                    + " damage.");
                    System.out.println("\t> You recive " + damageTaken + " damage by the " +   
                    enemy + " in retaliation");
    
                    if (playerHealth < 1) {
                        System.out.println("YOU DIED");
                        break; 
                    }
    
                }
    
                else if(input.equals("2")) {
                    if (numHealthPotions > 0) {
                    playerHealth = maxHealth;                    
                    numHealthPotions = numHealthPotions - 1;
                    System.out.println("You chug a holy health potion your HP is now " + 
                    playerHealth + " you have " + numHealthPotions + " health potions   
                    remaning");
                    }
                }
                    else {
                        System.out.println ("You have no health potions left, be careful");
                    }
                else if(input.equals("3")) {
                        if(enemyLevel > playerLevel + 3) {
                        System.out.println("You can not escape");
    
                        }
                }
    

Aucun commentaire:

Enregistrer un commentaire