带循环的菜单选项

时间:2017-09-20 14:46:23

标签: java

我在解决这部分问题时遇到了问题。即使看起来很容易我花了几天时间调试它。所以,我有一个包含6个选项的菜单。例如:

  1. 购买针好晚上!
  2. 购买quaffles
  3. 做服务套件
  4. 显示价目表
  5. 查看
  6. (我不确定是否需要将其写为if else语句)必要时再次执行循环。
  7. 一开始我尝试使用switch语句,但我无法使用一个案例中的值并在另一个案例中使用它。所以我试着写这个,但问题是,一旦我选择选项1,它将只显示选项1,无论我输入的是什么数字。

    answermenuoption = inScan.nextInt();
    
    while (answermenuoption != menustop) ;
    {
    
        if (answermenuoption == 1) {
            prompt = "Please enter another item from the menu above: ";
            if (answermenuoption == 1) {
    
                System.out.println("How many pins do you want to buy?");
    
                pins = inScan.nextInt();
    
                if (pins > 10) {
                    pricepins = pins * 18;
                    System.out.println("Thank you.Your order received");
    
                    System.out.println(menu);
                } else {
                    pricepins = pins * 20;
                    System.out.println("Thank you.Your order received");
                    System.out.println(menu);
                }
    
            } else if (answermenuoption == 2) {
                System.out.println("How many quaffles do you want to buy?");
                quaffles = inScan.nextInt();
    
                if (quaffles > 5) {
                    quafflesbox = quaffles / 5;
                    quafflesindividual = quaffles % 5;
                    pricequafflesbox = 580 * quafflesbox;
                    pricequafflesindividual = quafflesindividual * 145;
                    pricequaffles = quafflesbox * 580 + quafflesindividual * 145;
    
                    System.out.println("Thank you.Your order received");
                    System.out.println(menu);
                } else {
                    pricequaffles = quafflesindividual * 145;
                    System.out.println("Thank you.Your order received");
                    System.out.println(menu);
                }
    
            } else if (answermenuoption == 3) {
                System.out.println("How many broomstick service kit do you want to buy?");
                broomsticks = inScan.nextInt();
                pricebroomsticks = broomsticks * 899;
    
                System.out.println("Thank you.Your order received");
                System.out.println(menu);
            } else if (answermenuoption == 4) {
                System.out.println(pricemenudiscount);
                System.out.println(menu);
    
            } else if (answermenuoption == 5) pricetotal = pricequaffles + pricebroomsticks + pricepins;
    
            if (pricetotal > 1493) {
    
                discountapplied = pricetotal * 0.1;
                pricetotal = pricetotal - discountapplied;
    
                System.out.println("The total price is:" + pricetotal);
                System.out.println("Price of pins:" + pricepins);
                System.out.println("Price of quaffles" + pricequaffles);
                System.out.println("Price for quaffles in the box of 5 = " + pricequafflesbox);
                System.out.println("Price for single quaffles = " + pricequafflesindividual);
                System.out.println("Price for broomstick service kits = " + pricebroomsticks);
                System.out.println("Discount applied: = " + discountapplied);
    
                System.out.println("Please input the money amount: ");
                moneyinput = inScan.nextInt();
    
                changetocustomer = moneyinput - pricetotal;
                System.out.println("Your change is:" + changetocustomer);
            } else {
                System.out.println("The total price is:" + pricetotal);
                System.out.println("Price of pins:" + pricepins);
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您只能在循环开始时阅读answermenuoption

试试这个:

int answermenuoption = inScan.nextInt();
        do
        {
            if(answermenuoption == 1)
            {
             //insert code
            }
            if(answermenuoption==2)
            {
            //insert code
            }

            //....

        }while((answermenuoption=inScan.nextInt())!=menustop);

另请注意,此操作将始终至少运行一次。如果您希望它能够退出第一个值,只需在第一个if语句

之前添加以下代码
if(answermenuoption == menustop)
    break;