开关只是继续

时间:2014-03-20 15:35:06

标签: java if-statement switch-statement

我正在做一个简单的项目,只是为了让自己保持清醒。决定做一台自动售货机。好吧,我到最后并测试它。它只做了案例1和2,但是当我到案例3时,它或者在我选择零食选项时完成整个程序,或者如果我将它更改为if / else,在案例3中它将在我选择一个时停止运行小吃。它为什么这样做?我该怎么做才能解决它?

import java.util.Scanner;


public class Vending 
{
  public static void main(String[] args)
  {
    int choice;
    double deposit = 0;
    double total = 0;
    Scanner keyboard = new Scanner(System.in);
    boolean test = true;
    do
    {
        System.out.println("\nVending Machine Menu");
        System.out.println("\n \n1. View Items");
        System.out.println("2. Put Money into Machine");
        System.out.println("3. Select an Item");
        System.out.println("4.Recieve Change");
        System.out.println("5.Exit");
        System.out.println("\nPlease Make a Selection:  ");
        choice = keyboard.nextInt();

        switch(choice)
        {
        case 1 :
            System.out.println("\n 1. Fizzy soda : $0.50 \n 2. Gummi Possums : $0.25"
                    + "\n 3. Doggy Bones : $0.25 \n 4. Fruity Punch : $0.50");
            System.out.println("Press ENTER to continue");
            try{System.in.read();}
            catch(Exception e){}
            break;

        case 2:
            System.out.println("Enter amount to deposit: ");
            deposit = keyboard.nextDouble();
            total = total + deposit;
            break;

        case 3:
            Scanner keypad = new Scanner(System.in);
            System.out.println("\n 1. Fizzy soda : $0.50 \n 2. Gummi Possums : $0.25"
                    + "\n 3. Doggy Bones : $0.25 \n 4. Fruity Punch : $0.50");
            System.out.println("\nSelect an item: ");
            int snack = keypad.nextInt();
            if (total > 0)
            {
                if(snack == 1)
                {
                    if (total >= .5)
                    {
                        System.out.println("The Vending Machine dispenses a FIZZY SODA");
                        total = total - .5;
                    }
                    else 
                    {
                        System.out.println("Deposit more money");
                    }

                }
                else if(snack == 2)
                {
                    if (total >= .25)
                    {
                        System.out.println("The Vending Machine dispenses a package of GUMMI POSSUMS");
                        total = total - .25;
                    }
                    else
                    {
                        System.out.println("Deposit more money");
                    }
                }
                else if(snack == 3)
                {
                    if(total >= .25)
                    {
                        System.out.println("The Vending Machine Dispenses a package of DOGGY BONES");
                        total = total - .25;
                    }
                    else
                    {
                        System.out.println("Deposit more money");
                    }
                }
                else if(snack == 4)
                {
                    if(total >= .5)
                    {
                        System.out.println("The Vending Machine Dispenses a can of FRUITY PUNCH");
                        total = total - .5;
                    }
                    else
                    {
                        System.out.println("Deposit more money");
                    }
                }                       
            }
            else 
            {
                System.out.println("Please deposit some money");
            }
            keypad.close();

        case 4:
            System.out.println("You recieve " + total + " in change from the Vending Machine");
            total = 0;

        case 5:
            System.exit(0);
        }
    }
    while (test == true);
    keyboard.close();
  }
}

4 个答案:

答案 0 :(得分:3)

如果您不想执行下一个break语句,则需要在case语句末尾使用case

case 3:
    // do something
    break;   // break to avoid executing case 4
case 4:
    // ...

答案 1 :(得分:3)

如果你依赖休息时间;为了摆脱案件,那么在案例3,4或5中你没有中断陈述。

答案 2 :(得分:2)

您忘记在案件结束时添加break关键字,break会阻止执行下一个案例。

switch-case应该看起来像这个模板:

case 1:
    // do something
    break;
case 2:
    // do something
    break;   
default:
    // do something
    break;   

因此,请在每个案例的末尾添加break,例如:

case 4:
        System.out.println("You recieve " + total + " in change from the Vending Machine");
        total = 0;
        break;

在java中阅读有关switch-case的更多信息。

答案 3 :(得分:2)

乍一看:您的case s 3和4最后没有break声明。