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!");
}
}
}
}