简体   繁体   English

如何使用循环来允许用户选择多个项目?

[英]How do I use a loop to allow the user to select more than one item?

I am setting up a store menu, and I want the user to be able to select another item using a loop. 我正在设置商店菜单,我希望用户能够使用循环选择另一个项目。 I would like to ask "Do you want anything else?" 我想问“你还想要其他吗?” after the user selects an item, and for this to continue until the user says no. 用户选择一个项目后,此操作将继续直到用户拒绝。 I have not been able to figure out how to do this while using a loop. 我无法弄清楚在使用循环时如何执行此操作。

    System.out.println("\nHere are our products:");
    System.out.println("\t[L]arge Toothpick ---- $10.25");
    System.out.println("\t[M]edium Toothpick --- $ 5.25");
    System.out.println("\t[S]mall Toothpick ----   Free");

    final double cLTP = 10.25; // large toothpick
    final double cMTP = 5.25; // medium toothpick
    final double cSTP = 1.25; // small toothpick
    int QNTY; // quantity
    double TCOST; // total cost without tax
    double FCOST; // final cost with tax

    String response;
    char FL; // first letter
    System.out.println("\nWhat would you like to buy?");
    response = keyboard.nextLine();
    FL = response.charAt(0);

    if(FL == 'L' || FL == 'l')
    {
        System.out.println("How many?");
        QNTY = keyboard.nextInt();
        TCOST = QNTY * cLTP;
    }
    else if (FL == 'M' || FL == 'm')
    {
        System.out.println("How many?");
        QNTY = keyboard.nextInt();
        TCOST = QNTY * cMTP;
    }
    else if (FL == 'S' || FL == 's')
    {
        System.out.println("How many?");
        QNTY = keyboard.nextInt();
        TCOST = QNTY * cSTP;
    }           
}

Thanks to David Wallace and the commenters for helping me. 感谢David Wallace和评论者对我的帮助。 I have figured it out and the code is below: 我已经弄清楚了,代码如下:

if(FL == 'L' || FL == 'l')
    {
        System.out.println("How many?");
        QNTY = keyboard.nextInt();
        keyboard.nextLine();
        TCOST = QNTY * cLTP;

        System.out.println("Would you like to buy anything else?");
        response = keyboard.nextLine();
        FL = response.charAt(0);

        if(FL == 'N' || FL == 'n')
        {
            System.out.println("Okay then, your subtotal is: $"+TCOST);
            System.out.println("What is your sales tax? (Format: 5%, Enter: .05)");
            TAX = keyboard.nextDouble();
            FCOST = TCOST + TCOST * TAX;
            System.out.printf("Your total is: $%.2f\n", FCOST);
        }
        else
        {
            do
            {
                System.out.println("What would you like to buy?");  
                response = keyboard.nextLine();
                FL = response.charAt(0);

                if(FL == 'L' || FL == 'l')
                {
                    System.out.println("How many?");
                    QNTY = keyboard.nextInt();
                    keyboard.nextLine();
                    TCOST = TCOST + QNTY * cLTP;
                }
                if(FL == 'M' || FL == 'm')
                {
                    System.out.println("How many?");
                    QNTY = keyboard.nextInt();
                    keyboard.nextLine();
                    TCOST = TCOST + QNTY * cMTP;
                }
                if(FL == 'S' || FL == 's')
                {
                    System.out.println("How many?");
                    QNTY = keyboard.nextInt();
                    keyboard.nextLine();
                    TCOST = TCOST + QNTY * cSTP;
                }
                System.out.println("Would you like to buy anything else?");
                response = keyboard.nextLine();
                FL = response.charAt(0);
            }while(FL == 'y' || FL == 'Y');

            System.out.println("Okay then, your subtotal is: "+TCOST);
            System.out.println("What is your sales tax? (Format: 5%, Enter: .05)");
            TAX = keyboard.nextDouble();
            FCOST = TCOST + TCOST * TAX;
            System.out.printf("Your total is: $%.2f\n", FCOST);
        } 
    }  

The type of loop you want is a do-while loop. 您想要的循环类型是“ do-while”循环。 Insert 插入

do {

at the beginning of the part that you want to repeat. 在您要重复的部分的开头。

At the end, do the processing you require to get the answer from the user, and put the condition that you want to check inside a while , like this. 最后,进行所需的处理以从用户那里得到答案,并将您要检查的条件放入while ,就像这样。

} while (condition);

If the condition involves some variables that you're going to check (and I'm sure it will), you should declare them before the do , so that they don't go out of scope. 如果条件包含要检查的某些变量(并且我确定会),则应在do之前声明它们,以免它们超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个循环中分配多个同一字符串? - How do I assign more than one of the same String in a loop? 如何在javaFX中使用多个fxml? - How do I use more than one fxml with javaFX? 我将如何更改此代码以允许在客户端和服务器之间发送多个用户输入消息 - How would i change this code to allow more than one user input message to be sent between the client and server JSoup-选择多个项目 - JSoup - select more than one item 我如何不断询问用户有效的整数输入,如果输入有多个约束 - How do I keep asking a user for a valid integer input if there is more than one constraint to the input 我将如何让用户从该列表中选择一个项目并使其完成功能,而不是一个个地解析所有项目? - How would I let the user select an Item from this list and have it complete the function rather than parsing through all of them one by one? 如何使用带有多个字符串和arg的printf命令? - How do i use the printf command with more than one string and arg? 如何使用多个@RequestParam 注解? - How can I use more than one @RequestParam annotations? 如何在javafx 8中多次使用控件? - How can i use a control more than one time in javafx 8? 如何在活动中使用多个广播? - How can I use more than one Broadcast in activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM