1
\$\begingroup\$

I want a better way to do the if statements, and if there is a better way to do it. How is it more efficient? Is there something like a switch statement in Java that would help?

import java.util.Scanner;

public class main {



    private static boolean playing = false;
    private static int enemyHealth = 100;
    private static int playerHealth = 100;
    private static double enemyChoice = Math.ceil(Math.random() * 3);
    private static String pName;



    public static void checkHealth(){
        if(enemyHealth <= 0){
            System.out.println("Mother! "+pName+" wins!");
            playing = false;
        }else if(playerHealth <= 0){
            System.out.println(pName+" has fallen! Rocky wins!");
            playing = false;
        }
    }






    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);

        String wantPlay;
        // asking the player if they want to play
        System.out.println("Want to play a fighting game?: ");
        wantPlay = user_input.next();

        // if the player chooses to play, it wil lstart the game loop
        if(wantPlay.equals("Yes") || wantPlay.equals("yes")){

            System.out.println("What do you want your name to be?: ");
            pName = user_input.next();

            System.out.println("Hey folks!, were going to have an exiting game today!");
            System.out.println("In the left corner, we have Rocky Balboa!!!!!");
            System.out.println("and in the right corner, we have "+pName+"!!!!!");
            System.out.println("");

            playing = true;
        }

        while(playing == true){


            System.out.println("Rocky has "+enemyHealth+" health remaining.");
            System.out.println("You have "+playerHealth+" health remaining.");

            String action;
            System.out.println("What would you like to do? (a)upper cut (b)side punch (c)down cut: ");
            action = user_input.next();
            // a = 1     b = 2      c = 3
            enemyChoice = Math.ceil(Math.random() * 3);
            checkHealth();

            if(action.equals("a") && enemyChoice == 2){
                checkHealth();
                System.out.println("oohh! and "+pName+" throws a devistating blow into Rocky!");
                enemyHealth-=10;
                checkHealth();

            }else if (action.equals("a") && enemyChoice == 3){
                checkHealth();
                System.out.println("OH! "+pName+" JUST GOT SMASHED!");
                playerHealth -=10;
                checkHealth();
            }else if (action.equals("b") && enemyChoice == 3){
                checkHealth();
                System.out.println("Rocky just got smashed!!!!");
                enemyHealth-=10;
                checkHealth();
            }else if (action.equals("b") && enemyChoice == 1){
                checkHealth();
                System.out.println(pName+" better step up his game!");
                playerHealth-=10;
                checkHealth();
            }else if (action.equals("c") && enemyChoice == 1){
                checkHealth();
                System.out.println("Rockey just got pucnhed so hard that he might not even know he exists!");
                enemyHealth-=10;
                checkHealth();
            }else if (action.equals("c") && enemyChoice == 2){
                checkHealth();
                System.out.println(pName+" Just got smashed!");
                playerHealth -=10;
                checkHealth();
            }else if(action.equals("a") && enemyChoice == 1){
                System.out.println("Their fists collided! this is intense!");
            }else if(action.equals("b") && enemyChoice == 2){
                System.out.println("Their fists collided! this is intense!");
            }else if (action.equals("c") && enemyChoice == 3){
                System.out.println("Their fists collided! this is intense!");

            }



        }

    }

}
\$\endgroup\$
  • \$\begingroup\$ Yes. Switch exists in java too. You could ignore case of yes while checking equality \$\endgroup\$ – SacJn Jan 7 '16 at 4:36
4
\$\begingroup\$

You can do a Switch Statement to reduce the else-ifs you have.

Switch(action)
{
    case "a": Switch(enemyChoice)
              {
              case 2:
                   System.out.println("oohh! and "+pName+" throws a devistating blow into Rocky!");
                   enemyHealth-=10;
                   checkHealth();
              }
              case 3:
                   System.out.println("OH! "+pName+" JUST GOT SMASHED!");
                   enemyHealth-=10;
                   checkHealth();
              }
    case b:
etc....
}

Also is there any need to check health before you take away health? you remove health, then check, then disable play. you aren't alive then die before making an action I assume.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ yeah, i was still kind of testing the checkHealth thing, and realized that. that is why there is one at the top and bottom of each if/else if statement. i just never removed the first ones \$\endgroup\$ – LandotheProgrammer Jan 7 '16 at 14:44
  • \$\begingroup\$ Surely each of the cases in your switch statement requires a break keyword to terminate the enclosing case? Missing these will result in all of the following cases being executed \$\endgroup\$ – Adam Hodgson Jan 8 '16 at 20:14
4
\$\begingroup\$

Whenever you have a long list of if-else statements then you should consider whether an enum is more appropriate. In your case you seem to have some pretty clear logic that could be encapsulated better that way.

Here's an example to get your started:

public enum Action {
    UPPERCUT("a", 1, "Fists Collide", "Devastating Blow", "Blah blah"),
    JAB("b", 2, "Smashed", "Blocked", "Blah blah"),
    HAYMAKER("c", 3, "Blah", "blah", "blah");

    private final String code;
    private final int enemyBlockAction;
    private final String[] messages;

    Action(String code, int enemyBlockAction, String... messages) {
        this.code = code;
        this.enemyBlockAction = enemyBlockAction;
        this.messages = messages;
    }

    public static Action actionForCode(String code) {
        for (Action action: values()) {
            if (action.code.equals(code))
                return action;
        }
        throw new IllegalArgumentException("No action for code " + code);
    }

    public perform() {
        int enemyAction = random.nextInt(messages.length);
        System.out.println(messages[enemyAction]);
        if (enemyAction != enemyBlockAction) {
            enemyHealth -= 10;
            checkHealth();
        }
    }
}

You can then replace all your conditions with Action.actionForCode(code).perform()

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.