繁体   English   中英

无法访问的代码; 我不明白为什么它无法访问

[英]Unreachable code ; i dont understand why it is unreachable

    import java.util.Scanner;

public class start {

    public static void main(String[] args) {

        System.out.println("time to pick a name! type in your desired option");
        System.out.println("1: Harry");
        System.out.println("2: Billybob");
        System.out.println("3: Chuckle brother number 2");

        Scanner reader = new Scanner(System.in);

        byte input = Byte.parseByte(reader.nextLine());

        switch (input) {
        case 1:
            System.out.println("You have selected Harry");
            break;
        case 2:
            System.out.println("You have selected Billybob");
            break;
        case 3:
            System.out.println("You have selected Chuckle Brother number 2");
            break;

        default:
            System.out.println("Invalid Input");
            break;
        }
        System.out
                .println("time to head off. select which path you want to      take");
        System.out.println("1: the path heading north towards Winterfell");
        System.out.println("2: the path heading south, towards the red keep");
        System.out
                .println("3: the path heading East, towards the Moat Caillin");

        byte input2 = Byte.parseByte(reader.nextLine());

        switch (input2) {
        case 1:
            System.out
                    .println("you start for winterfell. the snows start strong on your way, and you die of exposure. Game Over");
            break;
        case 2:
            System.out
                    .println("you arrive at the red keep. you are attacked and you lose your money. you are now a beggar in fleebottom");
            System.out
                    .println("A magician offers you a position as his assistant. select which option you take");
            System.out.println("1: accept the magicians offer.");
            System.out.println("2: kindly decline the magicians offer.");
            System.out
                    .println("3: kill the magician and take his possessions.");
            byte input3 = Byte.parseByte(reader.nextLine());
            switch (input3) {
            case 1:
                System.out
                        .println("the magician takes you to his home, and kills you.   Game over.");
                break;
            case 2:
                System.out
                        .println("the magician is angry, he strikes you, and you die of an infected wound. Game over");
                break;
            case 3:
                System.out
                        .println("the magician has many expensive potions. You use these to get you into a position within the red keep");
                System.out
                        .println("King Tommen asks you to become a member of the small council. he lets you select your position.. select which option you take");
                System.out.println("1: become the master of ships.");
                System.out.println("2: become the master of coin.");
                System.out.println("3: become the master of whispers.");
                byte input5 = Byte.parseByte(reader.nextLine());

                switch (input5) {
                case 1:
                    System.out
                            .println("a storm hits a year later and you lose all of the capitals ships. you are dismissed of your position..");
                    break;
                case 2:
                    System.out
                            .println("the capital has no money. the iron bank looks to you for their debt that is owed. you are murdered. Game over!");
                    break;
                case 3:
                    System.out
                            .println("you find out secrets of highborn lords. you are a trusted acquaintance to the king.");
                    System.out
                            .println("the king is dying. he has no sons. he asks you who he should declare as the next king");
                    System.out.println("1: you should declare myself");
                    System.out.println("2: you should declare your uncle.");
                    System.out
                            .println("3: you should deide for yourself my king.");
                    byte input4 = Byte.parseByte(reader.nextLine());

                    switch (input4) {
                    case 1:
                        System.out
                                .println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
                        break;
                    case 2:
                        System.out
                                .println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
                        break;
                    case 3:
                        System.out
                                .println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
                        break;
                    default:
                        System.out.println("invalid input");
                    }

                    break;
                default:
                    System.out.println("Invalid Input");
                    break;
                }
                break;
            default:
                System.out.println("invalid input");

            }

            break;
        default:
            System.out.println("invalid input");

            break;

        case 3: {
            System.out
                    .println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
            System.out
                    .println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
            System.out.println("1: accept the lords offer.");
            System.out.println("2: kindly decline the lords offer.");
            System.out.println("3: kill the lord and take his possessions.");
            byte input4 = Byte.parseByte(reader.nextLine());

            switch (input4) {
            case 1:
                System.out
                        .println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
                break;
            case 2:
                System.out
                        .println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
                break;
            case 3:
                System.out
                        .println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
                break;
            default:
                System.out.println("invalid input");
            }

            break;
            Default: System.out.println("Invalid Input");
            break;
        }
        }
    }
}

“当它运行时,它说线程“main”中的异常java.lang.Error:未解决的编译问题:无法访问的代码

at start.main(start.java:143)"

我真的找不到它是如何无法到达的,我检查过大括号和各种各样的东西,任何帮助或提示将不胜感激

您在 break 语句后提到了“默认”标签,这就是问题所在

case 3: {
        System.out
                .println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
        System.out
                .println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
        System.out.println("1: accept the lords offer.");
        System.out.println("2: kindly decline the lords offer.");
        System.out.println("3: kill the lord and take his possessions.");
        byte input4 = Byte.parseByte(reader.nextLine());

        switch (input4) {
        case 1:
            System.out
                    .println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
            break;
        case 2:
            System.out
                    .println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
            break;
        case 3:
            System.out
                    .println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
            break;
        default:
            System.out.println("invalid input");
        }

        break;
        default: System.out.println("Invalid Input");
        break;
    }

观察最后3个陈述,你没有观察到一些可疑的东西吗?

休息; 语句将控制 case 语句,并且默认将永远没有机会执行

首先,分析你的代码,看看块

case 3: {
        System.out
                .println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
        System.out
                .println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
        System.out.println("1: accept the lords offer.");
        System.out.println("2: kindly decline the lords offer.");
        System.out.println("3: kill the lord and take his possessions.");
        byte input4 = Byte.parseByte(reader.nextLine());

        switch (input4) {
        case 1:
            System.out
                    .println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
            break;
        case 2:
            System.out
                    .println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
            break;
        case 3:
            System.out
                    .println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
            break;
        default:
            System.out.println("invalid input");
        }

        break;
    Default:
        System.out.println("Invalid Input");
        break;
    }

让我们分析一下这个块,你所拥有的是一堆打印语句,然后切换块,然后中断。 break 导致 tou 退出这个 case 语句,因此Default标签之后的代码是不可访问的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM