Java 断开开关中的标签

Java 断开开关中的标签,java,switch-statement,java-8,break,Java,Switch Statement,Java 8,Break,编辑: 谢谢大家的帮助。我能够使用我在前几章学到的技能和你的建议使它工作。非常感谢你 我决定尝试通过创建一个简单的文本冒险来巩固我从Java中学到的东西:初学者指南。我将开始第4章,其中涉及类和方法。前三章讨论了if、for、while、do-while、switch、简单键盘交互和break/continue 我计划在每一章结束后都回去编辑,以使用我学到的新技能。我几乎没有触及表面,我遇到了一个问题 // A basic, but hopefully, lengthy text adventu

编辑: 谢谢大家的帮助。我能够使用我在前几章学到的技能和你的建议使它工作。非常感谢你

我决定尝试通过创建一个简单的文本冒险来巩固我从Java中学到的东西:初学者指南。我将开始第4章,其中涉及类和方法。前三章讨论了if、for、while、do-while、switch、简单键盘交互和break/continue

我计划在每一章结束后都回去编辑,以使用我学到的新技能。我几乎没有触及表面,我遇到了一个问题

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

        }

}

    }
}
开关中默认语句的目的是将代码流带回类选择。我没有收到编译错误或运行时错误。当您选择除1、2、3或4之外的任何内容时。它说“不是一个有效的选择,请输入一个数字1-4”,就像它假设的那样,但是程序结束了


我不允许在交换机中使用这样的标签吗?或者它不工作是因为它在技术上超出了代码块?

我相信您在问题中描述的是某种goto功能,而不是Java中标签的工作方式

不幸的是,Java支持标签。这在中进行了描述

因此,基本上你可以有带标签的循环,你可以使用关键字
continue
break
等等来控制循环的流程

下面的示例演示了如何使用带有
break
关键字的循环。调用
break
时,它会终止带标签的语句,即
someLabel
后面的语句。它不会返回到指定标签的位置执行

someLabel:
    for (i = 0; i < 100; i++) {
        for (j = 0; j < 100; j++) {
            if (i % 20 == 0) {
                break someLabel;
            }
        }
    }
因此,基本上,如果您对
开关中的标签执行
中断
操作,您将中断整个语句(而不是跳转到语句的开头)

您可以通过运行下面的程序进一步测试标签。如果输入“quit”,它会中断while循环,否则它只会中断开关

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

就个人而言,我需要一个非常特殊的案例才能推荐使用标签。我发现,如果您改为重新排列代码,使其不需要标签(例如,将复杂的代码分解为较小的函数),那么代码就更容易理解

您可以将代码包含在
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

}

我认为将标签与
break
语句结合使用会使您走上错误的道路。您只需在开关中使用
break
语句,如果要避免程序退出,只需在
时使用
。下面是更新的代码

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

                }

            }
        }

    }
}

允许在Java中使用标签,但这不是一个好的做法。再想一想,避免使用不必要的异常,比如
IOException
,因为它不是由代码引发的

不,这不是有效的java语法-您可能应该使用while循环(
while(!validChoice){switch(){…}}
)我想修改一下,阅读一些更接近“几乎从不建议使用标签”的内容,因为很少有时候标签是最干净的解决方案,例如。,嵌套循环,其中将功能分解为方法使事情更难推理。“这是相对罕见的,但它们有时很容易处理。”达文尼顿,说得对。我把语气缓和了一点-谢谢!一旦我读完第4章,我肯定我就不需要标签声明了。现在,我可能只是等待。谢谢你的建议。这真的很有帮助。好吧,我下面的书总是在使用System.in.read()时添加抛出java.io.IOException;
// 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;

                }

            }
        }

    }
}