Label break in switch

Edited: Thanks for your help. I was able to get it to work using the skills that I learned in previous chapters and your advice. Thank you very much!

I decided to try cementing what I learned from Java: a beginner's guide by creating a simple text adventure. I'm going to start chapter 4, which includes classes and methods. The first three chapters dealt with, if, for now, do-while, switch, simple keyboard interaction and interrupt / continue.

I plan to come back after each chapter and edit it to use the new skills that I learned. I barely scratched the surface and I have a problem.

// 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 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;

        }

}

    }
}

- . . -, 1, 2, 3 4. : " , 1-4", , .

? , ?

+4
3

, , , - - goto, , Java.

Java, , . Oracle.

, , continue, break .. .

, break. break, , .. , someLabel. , .

someLabel:
    for (i = 0; i < 100; i++) {
        for (j = 0; j < 100; j++) {
            if (i % 20 == 0) {
                break someLabel;
            }
        }
    }

continue . , , continue someLabel; .

SO- :

BlockSegment:
if (conditionIsTrue) {
    doSomeProcessing ();
    if (resultOfProcessingIsFalse()) break BlockSegment;
    otherwiseDoSomeMoreProcessing();
    // These lines get skipped if the break statement
    // above gets executed
}
// This is where you resume execution after the break
anotherStatement();

, , , break switch, ( ).

, . while, "quit", .

public static void main(String... args) {
    programLoop:
    {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            final String input = scanner.next();
            switch (input) {
                case "quit":
                    break programLoop; // breaks the while-loop
                default:
                    break; // break the switch
            }
            System.out.println("After the switch");
        }
    }
}

, . , , , (, ).

+9

while , :

boolean validChoice=false;
while(!validChoice){
switch(choice){
    case 1:
        //logic
        validChoice=true;
    case 2:
        //logic
        validChoice=true;
    case 3:
        //logic
        validChoice=true;
    default:
        //print "invalid choice" and ask to reenter

}
0

, , break, . break , , while. .

// 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 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 tags in Java is allowed, but not good practice. Another example: Avoid using an unnecessary exception, such as IOExceptionsince it is not thrown by your code.

0
source

All Articles