[英]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.