switch语句循环中排除的数据计算

时间:2019-12-02 22:55:50

标签: java while-loop switch-statement nested-loops

我上课的最后一个项目是一个基于文本的游戏。到目前为止,除了1或2个问题之外,我都能按自己的意愿进行工作。第一个大问题是,当我进入“魔术”菜单(switch语句循环)时,所有数据都没有影响。这意味着,如果用户的HP降至0以下,则他们不会死亡,直到退出循环后才会发生。我已经弄乱了它,并使它正常工作,但是它破坏了代码的其他部分,所以请您帮忙。

我能够做的另一种解决方案是,在每个大小写之后将布尔值设置为false,使用户输入一个大小写,然后它跳出开关,效果很好,只不过它执行了整个开关循环,这意味着每个单输入。 这是主要代码,请记住它还有另一个类

package textRPG;
import java.util.Random;
import java.util.Scanner;

public class LoopyLoop {

    private static int maxEnemyHealth = 155;
    private static int enemyAttackDmg = 25;
    private static int health = 109;
    private static int attackDmg = 30;
    private static int mana = 15;
    private static int numHealthPots = 3;
    private static int healthPotionHealAmount = 30;
    private static int healthPotionDropChance = 20;
    private static int manaPotionRecoveryAmount = 12;
    private static int count = 0;
    private static int count2 = 0;
    private static int expUp = 0;
    private static int level = 1;   

    public static void main(String[] args) {

        SpellsAndAbilities spells = new SpellsAndAbilities();
        Scanner in = new Scanner(System.in);
        Random rand = new Random();
        //String Array for enemies, more can be added, or taken away
        String[] enemies = {"Skeleton", "Zombie", "Goblin", "Tiny Dragon", "Ben Afleck", "Little weird man", "Huge Bee", "Jeff", "Crazy Ray"};          
        //Set a variable running to true, to allow the program to run, so that if the user chooses to end the game early, they can do so
        boolean running = true;
        System.out.println("==================================================================");
        System.out.println("Welcome to Mysterical World of Mysterical Things!");
        System.out.println("Please press the number corresponding the action you wish to take.");   
        System.out.println("==================================================================");
        //Clause to keep the game running with invalid inputs
        GAME:           
        while(running) {

            int enemyHealth = rand.nextInt(maxEnemyHealth);
            String enemy = enemies[rand.nextInt(enemies.length)];
            System.out.println("\n\t### " + enemy + " has appeared! ###\n");

            while (enemyHealth > 0) {
                 System.out.println("\tYour HP: " + health);
                 System.out.println("\tYour Mana: " + mana);
                 System.out.println("\t" + enemy + "'s HP: " + enemyHealth);
                 System.out.println("\n\tWhat would you like to do?");
                 System.out.println("\t1. Attack");
                 System.out.println("\t2. Spells");
                 System.out.println("\t3. Use potion");
                 System.out.println("\t4. Run");
                 String input = in.nextLine();

                if (input.equals("1")) {
                    int damageDealt = rand.nextInt(attackDmg);
                    int damageTaken = rand.nextInt(enemyAttackDmg);
                    enemyHealth -= damageDealt;
                    health -= damageTaken;
                    System.out.println("\t>>> You hit the " + enemy + " for " + damageDealt + " damage <<<");
                    System.out.println("\t>>> The " + enemy + " hit you for " + damageTaken + " damage <<<");
                    System.out.println();
                    if(health < 1) {
                        System.out.println("\t=== You died! ===");
                        break;
                    }
                    //
                    //Problem Starts Here
                    //
                }else if (input.equals("2")) {
                    boolean set=true;
                    while(set) {                        
                        System.out.println("____________________________________________\n");
                        System.out.println("1. Fireblast (Cost: 6 mana, Damage: 10-19)");                   
                        System.out.println("2. Ice Sickle (Cost: 9 mana, Damage: 22-26)");                      
                        System.out.println("3. Whirlwind (Cost: 14 mana, Damage: 31-36)");                      
                        System.out.println("4. Earth shard (Cost: 28 mana, Damage: 38-42)");                        
                        System.out.println("5. Heal (Cost: 50 mana, Heals: 35 to 55 HP)");                      
                        System.out.println("6. Exit this menu");
                        System.out.println("____________________________________________");
                        int spellInput = in.nextInt();

                        switch (spellInput) {

                        case 1: if(mana >= 6) {
                            mana -= 6;
                            int damageDealt = spells.fireBlast();
                            int damageTaken = rand.nextInt(enemyAttackDmg);
                            enemyHealth -= damageDealt;
                            health -= damageTaken;                                                  
                            System.out.println("\t====================");
                            System.out.println("\tYou cast Fire Blast!");
                            System.out.println("\t====================");
                            System.out.println("\n\t>>> Fireblast hits the " + enemy + " for " + damageDealt + " damage <<<"); 
                            System.out.println("\t>>> You took " + damageTaken+ " damage<<<"); 
                            System.out.println();                   
                            break;
                            }
                            else {
                            break;
                            }                                                       

                        case 2: if (mana >= 9) {
                            mana -= 9;
                            int damageDealt = spells.iceSickle();
                            int damageTaken = rand.nextInt(enemyAttackDmg);
                            enemyHealth -= damageDealt;
                            health -= damageTaken;                              
                            System.out.println("\t====================");
                            System.out.println("\tYou cast Ice Sickle!");
                            System.out.println("\t====================");
                            System.out.println("\n\t>>> Ice Sickle slashes the " + enemy + " for " + damageDealt + " damage <<<"); 
                            System.out.println("\t>>> You took " + damageTaken+ " <<<"); 
                            System.out.println();
                            break;
                        }
                        else {
                            System.out.println("You need more mana");
                            break;                                       
                        }   

                        case 3: if(mana >= 14) {
                            mana -= 14;     
                            int damageDealt = spells.whirlWind();
                            int damageTaken = rand.nextInt(enemyAttackDmg);
                            enemyHealth -= damageDealt;
                            health -= damageTaken;                      
                            System.out.println("\t====================");
                            System.out.println("\tYou cast Whirlwind!");
                            System.out.println("\t====================");
                            System.out.println("\n\t>>> Whirlwind ravages " + enemy + " for " + damageDealt + " damage <<<"); 
                            System.out.println("\t>>> You took " + damageTaken+ " <<<"); 
                            System.out.println();
                            break;
                        }
                        else {
                            break;
                        }


                        case 4: if(mana >= 28) {
                            mana -= 28; 
                            int damageDealt = spells.earthShard();
                            int damageTaken = rand.nextInt(enemyAttackDmg);
                            enemyHealth -= damageDealt;
                            health -= damageTaken;                      
                            System.out.println("\t====================");
                            System.out.println("\tYou cast Earth Shard!");
                            System.out.println("\t====================");
                            System.out.println("\n\t>>> Earth shard pummels " + enemy + " for " + damageDealt + " damage <<<"); 
                            System.out.println("\t>>> You took " + damageTaken+ " <<<"); 
                            System.out.println();
                            break;
                        }
                        else {
                            break;
                        }


                        case 5: if(mana >= 50) {
                            mana -= 50;                             
                            health += spells.Heal();
                            int damageTaken = rand.nextInt(enemyAttackDmg);                                 
                            health -= damageTaken;              
                            System.out.println("\t===============");
                            System.out.println("\tYou cast Heal!");
                            System.out.println("\t===============");
                            System.out.println("\n\t>>> You recover " + spells.Heal() + " HP <<<"); 
                            System.out.println("\t>>> You took " + damageTaken+ " damage <<<");
                            System.out.println("\tYour HP: " + health);
                            System.out.println("\tYour Mana: " + mana);
                            System.out.println();
                            break;
                        }
                        else {
                            break;;
                        }


                        case 6: set = false;

                        default: break;                 
                        }
                    }
                }

                else if (input.equals("3")) {   

                        if (numHealthPots > 0) {
                            count2++;
                            health += healthPotionHealAmount;
                            mana += manaPotionRecoveryAmount;
                            numHealthPots--;
                            System.out.println("\tYou drink a potion, healing for " + healthPotionHealAmount + " HP and recovering " + manaPotionRecoveryAmount + " mana" +
                                               "\n\t>> You now have " + health + " HP and " + mana +" Mana <<" +
                                               "\n\t>> You have " + numHealthPots + " potions left <<");
                        }

                        else {
                            System.out.println("\tYou have no potions left! Defeat enemies for a chance to get one!");
                        }

                } else if (input.equals("4")) {             
                            System.out.println("\tYou run away from the " + enemy + "!");
                    continue GAME;
                  }

                  else {                        
                        System.out.println("-------------------------------------");
                  }
            }

            if(health < 1) {
                System.out.println("You pitifully limp out of the dungeon, too weak to go on!");
                break;
            }
                count++;
                System.out.println("=========================================");
                System.out.println(" *** " + enemy +" was defeated! ***");
                System.out.println("### You have " + health + " HP ### \n### And " + mana + " mana ###");
                expUp += expUp + 25;            

            if (rand.nextInt(100) < healthPotionDropChance) {

                numHealthPots++;
                System.out.println(" # The " + enemy + " dropped a health potion");
                System.out.println(" # You now have " + numHealthPots + " health potion(s) # ");
            } 
                if(expUp > 99) {
                    level++;
                    health = health+22;
                    attackDmg = attackDmg + 2;
                    mana = mana + 7;
                    System.out.println("****** LEVEL UP!!! ******");
                    System.out.println("You are now level " + level + "!");
                    System.out.println("Each level up rewards 22 to total health, +2 to attack, and +7 to mana!");
                }
                System.out.println("=========================================");
                System.out.println("What would you like to do now?");
                System.out.println("Press 1 to continue fighting");
                System.out.println("Pess 2 to exit");

            String input = in.nextLine();

            while (!input.equals("1") && !input.equals("2")) {
                System.out.println("Invalid command");
                input = in.nextLine();
            }
            if (input.equals("1")) {
                System.out.println("You choose to continue, good luck!");
            }
            else if (input.equals("2")) {
                System.out.println("You exit the game");
                break;
            }
        }
                System.out.println("####################");
                System.out.println("Thanks for playing!");
                System.out.println("####################");
                System.out.println("Press 5 to see your score!");
                String input2 = in.nextLine();
                if (input2.equals("5")){
                    System.out.println("You defeated " + count + " enemies");
                    System.out.println("Max level achieved: " + level);
                    System.out.println("And you used " + count2 + " potions");                  
                }
                    else {
                        System.out.println("Invalid input");
                        input2 = in.nextLine();
                    }
                in.close();
        }
}

输出最终看起来像这样:

6. Exit this menu
____________________________________________
6
=========================================
 *** Tiny Dragon was defeated! ***
### You have 136 HP ### 
### And 6 mana ###
****** LEVEL UP!!! ******

0 个答案:

没有答案