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

时间:2014-02-18 21:30:44

标签: java

我正在设置商店菜单,我希望用户能够使用循环选择另一个商品。我想问“你还想要别的吗?”在用户选择项目之后,为此继续直到用户拒绝。我在使用循环时无法弄清楚如何做到这一点。

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

1 个答案:

答案 0 :(得分:5)

你想要的循环类型是一个do-while循环。插入

do {

在您要重复的部分的开头。

最后,执行您需要的处理以获得用户的答案,并将您要检查的条件放在while内,如下所示。

} while (condition);

如果条件涉及你要检查的一些变量(我确信它会),你应该在do之前声明它们,这样它们就不会超出范围。 / p>