Break label in switch

后端 未结 3 1031
既然无缘
既然无缘 2021-01-19 04:42

Edited: Thank you all for your help. I was able to get it working using the the skills I learned in the previous chapters and your advice. Thank you so much!

3条回答
  •  我在风中等你
    2021-01-19 04:51

    I think that using label combined to the break statement put you on the wrong way. You can simply use the break statement in the switch and if you want to avoid the program exit, simply use a while. Below the updated code.

    // A basic, but hopefully, lengthy text adventure.
    
    import java.util.Scanner;
    
    class TextAdventure
    {
        public static void main(String args[])
        {
            System.out.println("\t\t BASIC TEXT ADVENTURE");
    
    
            // variables I need, attributes, classes, character name, player's choice, gold
            int str = 0, inte = 0, chr = 0, con = 0, dex = 0, gold;
            char charName, choice;
    
            System.out.println("Welcome player! You are about to embark upon a quest in the form of a text adventure.");
            System.out.println("You will make choices, fight monsters, and seek treasure. Come back victorious and you");
            System.out.println("could quite possibly buy your way into nobility!");
            System.out.println();
    
            boolean toEnd = false;
            while(!toEnd) {
    
                {
                    System.out.println("Please select your class:");
                    System.out.println("1. Warrior");
                    System.out.println("2. Mage");
                    System.out.println("3. Rogue");
                    System.out.println("4. Archer");
    
                    Scanner scanner = new Scanner(System.in);
                    choice = scanner.next().charAt(0); // Get players choice of class
    
                    toEnd = true;
    
                    switch (choice) {
                        case '1':
                            System.out.println("You have chosen the Warrior class!");
                            System.out.println("You're stats are as followed:");
                            System.out.println("Str: 16");
                            System.out.println("Int: 11");
                            System.out.println("Chr: 14");
                            System.out.println("Con: 15");
                            System.out.println("Dex: 9");
                            str = 16;
                            inte = 11;
                            chr = 14;
                            con = 15;
                            dex = 9;
    
                            break;
    
                        case '2':
                            System.out.println("You have chosen the Mage class!");
                            System.out.println("You're stats are as followed:");
                            System.out.println("Str: 16");
                            System.out.println("Int: 11");
                            System.out.println("Chr: 14");
                            System.out.println("Con: 15");
                            System.out.println("Dex: 9");
                            str = 9;
                            inte = 16;
                            chr = 14;
                            con = 15;
                            dex = 11;
    
                            break;
    
                        case '3':
                            System.out.println("You have chosen the Rogue class!");
                            System.out.println("You're stats are as followed:");
                            System.out.println("Str: 16");
                            System.out.println("Int: 11");
                            System.out.println("Chr: 14");
                            System.out.println("Con: 15");
                            System.out.println("Dex: 9");
                            str = 15;
                            inte = 11;
                            chr = 14;
                            con = 9;
                            dex = 16;
    
                            break;
    
                        case '4':
                            System.out.println("You have chosen the Archer class!");
                            System.out.println("You're stats are as followed:");
                            System.out.println("Str: 16");
                            System.out.println("Int: 11");
                            System.out.println("Chr: 14");
                            System.out.println("Con: 15");
                            System.out.println("Dex: 9");
                            str = 9;
                            inte = 11;
                            chr = 14;
                            con = 15;
                            dex = 16;
    
                            break;
    
                        default:
                            System.out.println("Not a valid choice, please enter a digit 1-4");
                            toEnd = false;
                            break;// caseChoice;
    
                    }
    
                }
            }
    
        }
    }
    

    Using label in Java is allowed but not a good practice. One more think, avoid to use unnecessary exception like IOException since is not thrown by your code.

提交回复
热议问题