简体   繁体   中英

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

"when it runs it says Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable code

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

i seriously cannot find how it is unreachbale, ive checked braces and all sorts, any help or tips would be appreciated

you have mentioned the 'Default' label after break statement, thats the problem

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

observe the last 3 statements, dont you observe something fishy there?

break; statement will take the control out of case statement, and the default will never get a chance to execute

First, analyse your code, Have a look at block

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

Let's analyse this block, what you have, is bunch of print statements, then switch block, and break after that. break cause tou to exit from this case statement, hence code after Default label is unreachable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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