Java Switch Statement

后端 未结 8 2228
南笙
南笙 2021-02-07 13:48

I have a problem using switch statement when I tried to deal with a special situation. For example, I have 3 cases: A, B, C.

  • for A, I want to do statement_1 and st
相关标签:
8条回答
  • 2021-02-07 14:26

    When you use "case," use a switch statement first, and make sure you've got an initialized variable in it. Here's an example.

    Use an initialized variable: i.e.

    (String s=sc.nextLine();)
    
            switch (s) {
            case "Pepperoni":
                System.out.println("Pepperoni? Nice choice. Please wait............Ding! Here's your pepperoni pizza. Enjoy, and thanks for using the Local Pizzeria!");
            case "Cheese":
                System.out.println("Cheese? Okay! Soft cheese it is! Please wait............Ding! Here's your cheese pizza. Enjoy, and thank you for using the Local Pizzeria!");
                break;
            case "Mushrooms":
                System.out.println("Mushrooms? Veggie person, right? OK! Please wait............Ding! Here's your mushroom pizza. Enjoy, and thanks for using the Local Pizzeria!");
                break;
            case "Beef Bits":
                System.out.println("Beef bits? Like meat? Alright! Please wait............Ding! Here's your beef bit pizza. Enjoy, and thank you for using the Local Pizzeria!");
    
                break;
            case "Spicy Buffalo Chicken":
                System.out.println("Spicy buffalo chicken? Way to spice things up! Please wait............Ding! Here's your spicy buffalo chicken pizza. Enjoy, and thank you for using the Local Pizzeria!");
                break;
            case "Exit":
                System.out.println("See you soon!");
            }
        }
    
    0 讨论(0)
  • 2021-02-07 14:27
    switch (variable) {
    case A:  
    do statement_1;
    do statement_3;
    break;
    case B:
    do statement_2;
    do statement_3;
    break;
    }
    
    0 讨论(0)
提交回复
热议问题