簡體   English   中英

如何使用循環來允許用戶選擇多個項目?

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

我正在設置商店菜單,我希望用戶能夠使用循環選擇另一個項目。 我想問“你還想要其他嗎?” 用戶選擇一個項目后,此操作將繼續直到用戶拒絕。 我無法弄清楚在使用循環時如何執行此操作。

    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;
    }           
}

感謝David Wallace和評論者對我的幫助。 我已經弄清楚了,代碼如下:

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);
        } 
    }  

您想要的循環類型是“ do-while”循環。 插入

do {

在您要重復的部分的開頭。

最后,進行所需的處理以從用戶那里得到答案,並將您要檢查的條件放入while ,就像這樣。

} while (condition);

如果條件包含要檢查的某些變量(並且我確定會),則應在do之前聲明它們,以免它們超出范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM