Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这种转变还在继续_Java_If Statement_Switch Statement - Fatal编程技术网

Java 这种转变还在继续

Java 这种转变还在继续,java,if-statement,switch-statement,Java,If Statement,Switch Statement,我正在做一个简单的项目,只是为了让自己保持新鲜。决定做一台自动售货机。好吧,我坚持到底,并测试它。它在案例1和案例2中运行得很好,但当我进入案例3时,它要么在我选择零食选项时完成整个程序,要么在案例3中将其更改为if/else,当我选择零食时,它将停止运行。为什么要这样做?我能做些什么来修复它 import java.util.Scanner; public class Vending { public static void main(String[] args) { i

我正在做一个简单的项目,只是为了让自己保持新鲜。决定做一台自动售货机。好吧,我坚持到底,并测试它。它在案例1和案例2中运行得很好,但当我进入案例3时,它要么在我选择零食选项时完成整个程序,要么在案例3中将其更改为if/else,当我选择零食时,它将停止运行。为什么要这样做?我能做些什么来修复它

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

您忘记在案例末尾添加
break
关键字,break将阻止执行下一个案例

开关盒应类似于此模板:

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的更多信息。

如果您忘记在案例末尾添加
break
关键字,break将阻止执行下一个案例

开关盒应类似于此模板:

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的更多信息。

乍一看:您的
案例
s3和4结尾没有
break
语句。

乍一看:您的
案例
s3和4结尾没有
break
语句。

如果不想,您需要在
案例
语句结尾使用
break
执行下一个
case
语句:

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

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

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

如果你是依靠休息;若要脱离案例,则案例3、4或5中没有中断语句。

如果您依赖中断;为了摆脱这种情况,在情况3、4或5中没有break语句。

不需要将test与true进行比较,比如
while(test==true)
可以直接做
while(test)谢谢。我要澄清的是,这里不需要比较test和true,比如
while(test==true)
可以直接做
while(test)谢谢。我会把它清理干净谢谢你。我甚至没看到我忘了休息。谢谢。我甚至没看到我忘记休息了。