Break Label im Schalter

Bearbeitet: Vielen Dank für Ihre Hilfe. Ich konnte es mit den Fähigkeiten zum Laufen bringen, die ich in den vorherigen Kapiteln und Ihrem Rat gelernt habe. Ich danke dir sehr

Ich habe beschlossen, die Dinge, die ich aus Java: A Beginner's Guide gelernt habe, zu zementieren, indem ich ein einfaches Textabenteuer erstellte. Ich beginne gerade Kapitel 4, in dem es um Klassen und Methoden geht. In den ersten drei Kapiteln wurde behandelt, ob, für, während, während, wechseln, einfache Tastaturinteraktion und Pause / Fortfahren.

Ich plane, nach jedem Kapitel zurückzugehen und es zu bearbeiten, um die neuen Fähigkeiten zu nutzen, die ich gelernt habe. Ich habe die Oberfläche kaum zerkratzt und bin auf ein Problem gestoßen.

// A basic, but hopefully, lengthy text adventure.

class TextAdventure
{
    public static void main(String args[])
    throws java.io.IOException
    {
        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();


caseChoice: {       
        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");

        choice = (char) System.in.read(); // Get players choice of class



        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");
                break caseChoice;

        }

}

    }
}

Die Absicht der Standardanweisung in der Option besteht darin, den Codefluss wieder auf die Klassenauswahl zurückzusetzen. Ich erhalte KEINEN Kompilierungs- oder Laufzeitfehler. Wenn Sie etwas anderes als 1, 2, 3 oder 4 auswählen, wird wie angenommen "Keine gültige Auswahl, bitte geben Sie eine Ziffer 1-4 ein" angezeigt, das Programm wird jedoch beendet.

Darf ich ein solches Label nicht in einem Switch verwenden? Oder funktioniert es nicht, weil es technisch außerhalb des Codeblocks liegt?

Antworten auf die Frage(6)

Ihre Antwort auf die Frage