如何在switch语句上设置do..while循环并添加输入



我目前正在做一个项目,该项目必须取一个整数1-10,将其切换为一个二重,然后再乘以一个数量。然而,所有这些都必须由用户输入,就像你在餐馆点东西一样。我遇到的问题是得到上面的一个循环,它存储了总数(价格*数量),然后将其相加。exp:1*5=5+4.5*2=9=总共14。我必须循环到用户可以通过输入终止循环的程度。我试着通过在开关上添加一个计数器++来实现这一点,但我所做的似乎都不起作用。以下是我正在处理的当前代码。

因此,总而言之,我需要在下面循环(直到提示不要),并不断地将总数相加,直到我在System.out.println("total");行上得到最后一个总数

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Menu;
import java.util.Scanner;
/**
 *
 * @author -------
 */

public class Menu {

    public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    Scanner keyboard = new Scanner (System.in);
    double price = 0;
    int ItemNum;
    int quantity;
    double change;
    double total;
    double payment;
    int counter = 1;

    System.out.println("Welcome to Home Italliano!nHit enter for menu.");
    keyboard.nextLine();

    System.out.println("Menu" +"n"+"(1)Noodles - 1.15$"+"n"+"(2)Pizza Slice - 3.00$"
        +"n"+"(3)Lasagna - 4.99$"+"n"+"(4)Beverage - 2.25"+"n"+"(5)Md. Pizza - 7.85$"+"n"+
        "(6)Lg. Pizza - 11.10$"+"n"+"(7)Calzone - 3.30$"+"n"+"(8)Garlic Knot - 1.25$"
        +"n"+"(9)Rg. Pasta - 8.00$"+"n"+"(10)Triple Meat Pasta - 9.99$");
    while (counter ==1)
    System.out.println("Please input the number of the item you wish to order. hit 0 for total.");
    ItemNum = input.nextInt();
    System.out.println("Please enter the quantity for this item");
    quantity = input.nextInt();
    total = price * quantity;
    switch (ItemNum){
        case 0:
            counter++;
        case 1:
            price = 1.15;
            break;
        case 2:
            price = 3.00;
            break;
        case 3:
            price = 4.99;
            break;
        case 4:
            price = 2.25;
            break;
        case 5:
            price = 7.85;
            break;
        case 6:
            price = 11.10;
            break;
        case 7:
            price = 3.30;
            break;
        case 8:
            price = 1.25;
        case 9:
            price = 8.00;
            break;
        case 10:
            price = 9.99;
            break;
        default:
                System.out.println("Please enter a menu item.");
    } 
    System.out.println(total);

        }
            }    

一些提示:

  • 将您的逻辑封装在一个无限循环中(即while(true))
  • 注意循环中的花括号
  • 使用特定的intScanner.nextLine来获得输入的String表示(即"q"表示"退出"),如果匹配,则break表示无限循环(在这种情况下,您需要一些String->int转换)
  • 是"Italiano",一个"l"

while循环没有任何大括号:

while (counter ==1)
正如其他人所指出的:while循环缺少大括号,因此只影响第二行。

此外:在您通过切换条款确定价格之前,您正在访问价格。

虽然如果你添加大括号可能会起作用,但你的解决方案应该更像这样:

1、为菜单上的项目创建一个私有类。这样的MenuItem会有名称和价格。

2,然后创建一个MenuItems的java.utils.HashMap。然后用菜单初始化这个映射。Map将给定的Key(在本例中为menuNumber)映射到给定的Object(在本案中为MenuItem Object)

例如:

menuMap.put(1, new MenuItem("Noodles", Double.valueOf("1.15"));

3、做初始输出;

System.out.println("Menu:");
for(String key: menuMap.keySet())
{
    System.out.println(menuMap.get(key).toString());
}

4、创建一个while循环,就像这样的

//Initially Scan for and validate ItemChoice
while(!inputMenuNumber == 'Exit Signal')
{
    //Scan for and validate Input Amount
    MenuItem chosenItem = menuMap.get(inputMenuNumber);
    total = chosenItem.getItemPrice() * Integer.valueOf(inputAmount);
    //Scan for and validate ItemChoice
}
Sytem.out.println(total);

它使代码更易于阅读和理解。

如果你有任何问题,请毫不犹豫地问。

最新更新