我的switch语句出了什么问题 - 显示了多个案例?

时间:2015-06-19 00:02:56

标签: java case

以下是代码:

public static void menu() {
        int choice;
        final int lsPrice = 120;
        final int ssPrice = 90;
        final int mPrice = 80;
        final int mrPrice = 150;
        final int hpPrice = 10;
        System.out.println("Here are the prices. Enter your choice.");
        System.out.printf("1. Long Sword  %7d\n", lsPrice);
        System.out.printf("2. Short Sword  %6d\n", ssPrice);
        System.out.printf("3. Mace  %13d\n", mPrice);
        System.out.printf("4. Magic Ring  %7d\n", mrPrice);
        System.out.printf("5. Healing Potion  %3d\n",hpPrice);
        Scanner keyboard = new Scanner(System.in);
        int item = keyboard.nextInt();
        switch (item) {
            case 1:
                choice = lsPrice;
                System.out.println("Your choice costs " + choice);
            case 2:
                choice = ssPrice;
                System.out.println("Your choice costs " + choice);
            case 3:
                choice = mPrice;
                System.out.println("Your choice costs " + choice);
            case 4:
                choice = mrPrice;
                System.out.println("Your choice costs " + choice);
            case 5: 
                choice = hpPrice;
                System.out.println("Your choice costs " + choice);
            default: 
                System.out.println("Invalid choice!");

这是我选择案例3时的输出:

Your choice costs 80
Your choice costs 150
Your choice costs 10
Invalid choice!
Enter the quantity that you wish to purchase.

我不知道为什么只要我选择案例3,案例3,4,5和默认值就出现了。我只是希望它打印出我在我指定的情况下选择的内容,而不是全部案件。

0 个答案:

没有答案