gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:16:25 25 4
gpt4 key购买 nike

我正在设置一个商店菜单,我希望用户能够使用循环选择另一个项目。我想问“你还想要什么吗?”用户选择一个项目后,此过程将持续到用户拒绝为止。我无法弄清楚如何在使用循环时执行此操作。

    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 之前声明它们,这样它们就不会超出范围范围。

关于java - 如何使用循环允许用户选择多个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21865812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com