Switch / Case Java返回主菜单

时间:2015-02-23 22:16:13

标签: java methods switch-statement return case

我正在为我的Java类编写一个程序,我需要制作一个像输出一样的收据。我必须允许用户从3个项目中进行选择。选择项目后,他们选择数量,然后将其乘以价格,得出总数。用户需要选择所有3个项目,但可以按任何顺序选择它们。我正在尝试使用Switch / Case方法来帮助我将变量分配给用户选择的内容。这就是我目前所做的:

System.out.println("Enter name of First item");
System.out.println(" 1. Gum, 2. Soda, 3. Chips");
firstItem = keyboard.nextInt();
switch (firstItem) {
    case 1:
        System.out.println("Gum");
        System.out.println(gumPrice);
        System.out.println("How many packs of gum would you like to purchase?");
        gumBought = keyboard.nextInt();
        break;
    case 2:
        System.out.println("Soda");
        System.out.println(sodaPrice);
        System.out.println("How many cups of soda would you like to purchase?");
        sodaBought = keyboard.nextInt();
        break;
    case 3:
        System.out.println("Chips");
        System.out.println(chipsPrice);
        System.out.println("How many bags of chips would you like to purchase?");
        chipsBought = keyboard.nextInt();
        break;
    default:
        System.out.println("Unknown Entry");
        break;
}

然后它开始第二项Switch / Case。如果用户选择1-3之外的数字或两次选择相同的数字/项目,我需要知道如何让它返回主要问题(口香糖/苏打/芯片)。我怎样才能有效地做到这一点?

另外,我可以使用Strings而不是Int来允许用户键入" Gum"而不是" 1"?感谢。

2 个答案:

答案 0 :(得分:1)

我对提供一个完整的工作示例犹豫不决,因此我将为您提供一些代码段,以帮助您使用Strings

首先,如果您想重复算法直到满足条件,您可以使用loop为您提供所搜索的内容。

while (condition) {
    //do something
}

自Java 7发布以来,switch-case可以与字符串一起使用。您只需要更改输入类型以匹配您想要的内容:

System.out.print("Enter name of your item [gum] [soda] [chips]: ");
String choice = keyboard.next().toLowerCase();

然后您必须修改switch-case语法。

switch (choice) {//now String
    case "gum":
    case "soda":
    case "chips":
    default:
}

希望这会让你开始。

答案 1 :(得分:0)

  • 让用户选择退出'9'
  • 将switch语句放在while()循环

这应该有效:

public static void main(String[] args) {

    double gumPrice, sodaPrice, chipsPrice;

    gumPrice = 1.25;
    sodaPrice = 4.00;
    chipsPrice = 3.25;

    while(true){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter name of First item");
    System.out.println(" 1. Gum, 2. Soda, 3. Chips, 9. EXIT");
    int firstItem = keyboard.nextInt( );

    switch (firstItem){
        case 1:
           System.out.println("Gum");
           System.out.println(gumPrice);
           System.out.println("How many packs of gum would you like to purchase?");
           int gumBought = keyboard.nextInt();
           break;
        case 2:
           System.out.println("Soda");
           System.out.println(sodaPrice);
           System.out.println("How many cups of soda would you like to purchase?");
           int sodaBought = keyboard.nextInt();
            break;
        case 3:
           System.out.println("Chips");
           System.out.println(chipsPrice);
           System.out.println("How many bags of chips would you like to purchase?");
           int chipsBought = keyboard.nextInt();
           break;
        case 9:
           return;
        }
    }
}