切换输入问题

时间:2017-11-28 20:38:02

标签: java switch-statement

好的,所以我显然没有正确地问这个问题。该想法是向用户询问与风味相关的数字(指示他们想要的风味冰淇淋)。每当我输入任何数字时,它都会吐出默认值。其中一个问题(至少我认为是......)是input = keyboard.nextInt();.

System.out.println(" ");
    System.out.println(" What flavor of ice cream would you like? ");
    System.out.println("(1) Vanilla  (2) Peanut Butter  (3) Chocolate  (4)Chocolate Chip  (5) Mint Chocolate Chip " +
        " (6) Cookie Dough  (7) Mint Mouse tracks  (8) Coconut  (9) Pinapple  (10) Cotton Candy" +
            "(11) Mouse Tracks (12) Oreo Cookies and Cream");
    input = keyboard.nextInt();
    flavorType = input.charAt(0);

    switch(flavorType)
    {
        case 1:
            order.setFlavor ("Vanilla");
            break;
        case 2:
            order.setFlavor("Peanut-Butter");
            break;
        case 3:
            order.setFlavor ("Chocolate");
            break;
        case 4:
            order.setFlavor ("Chocolate Chip");
            break;
        case 5:
            order.setFlavor ("Mint Chocolate Chip");
            break;
        case 6:
            order.setFlavor ("Choclate Chip Cookie Dough");
            break;
        case 7:
            order.setFlavor ("Mint Mouse Tracks");
            break;
        case 8:
            order.setFlavor ("Coconut");
            break;
        case 9:
            order.setFlavor ("Pinapple");
            break;
        case 10:
            order.setFlavor ("Cotton Candy");
            break;
        case 11:
            order.setFlavor ("Mouse Tracks");
            break;
        case 12:
            order.setFlavor ("Oreo Cookies and Cream");
            break;
        default:
        System.out.println(" ");
        System.out.println(" Im sorry, that was not one of the choices, you will get two scoops " +
            " of vanilla with whipped cream,hot fudge, rainbow sprinkles, and a cherry");
            order.setFlavor ("Vanilla");
    }

现在,我也将变量声明为最重要的

        String customerName;
    double cost;
    double toppingCost = 1.25;
    int numberOfScoops;
    int numberOfDeluxe = 0;
    int numberOfToppings = 0;
    //String flavor;
    String toppingList;
    final double TAX_RATE = .08625;
    double tax;
    char choice;
    char flavorType;
    String input;
    String toppings =  " Whipped cream, syrup, and sprinkles ( Chocolate or rainbow)";
    int toppingChoice;

我也有驱动程序......

public void setFlavor (String type)
{
     flavor = type;
}

这只是作业的一部分。其余部分涉及勺子,浇头等等。但这部分是问题,无法使开关声明正确。

1 个答案:

答案 0 :(得分:0)

假设您宣布:

Scanner keyboard = new Scanner(System.in);

order也被正确定义。正如我在评论中写的那样 - 您将input视为int,但在您尝试致电String时将其视为input.charAt

此外,您可以使用Map来简化代码,以找出选择(而不是case-switch)以及显示选项。

以下代码适用于Java 9:

    Map<Integer, String> dict = new TreeMap(Map.of(1, "Vanilla",
                                                   2, "Peanut-Butter",
                                                   3, "Chocolate",
                                                   4, "Chocolate Chip",
                                                   5, "Mint Chocolate Chip",
                                                   6, "Choclate Chip Cookie Dough",
                                                   7, "Mint Mouse Tracks",
                                                   8, "Coconut",
                                                   9, "Pinapple",
                                                   10, "Cotton Candy"));

    // Map.of API allows up to 10 items - so we'll need to add the other two manually. 
    // That's also the reason that we're copying the Map.of by doing:
    // new TreeMap(Map.of(...)) - since `Map.of` returns an immutable Map which we cannot modify
    dict.put(11, "Mouse Tracks");
    dict.put(12, "Oreo Cookies and Cream");

    System.out.println("\n What flavor of ice cream would you like? ");
    dict.forEach((k, v) -> System.out.println(String.format("(%d) %s", k, v)));

    Scanner keyboard = new Scanner(System.in);
    int input = keyboard.nextInt();

    if (null == dict.get(input)) {
        System.out.println(" Im sorry, that was not one of the choices, you will get two scoops " +
                               " of vanilla with whipped cream,hot fudge, rainbow sprinkles, and a cherry");
        order.setFlavor ("Vanilla");
    } else {
        order.setFlavor (dict.get(input));
    }
相关问题