Reputation: 5
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. 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);
}
}
Upvotes: 0
Views: 71
Reputation: 79847
The type of loop you want is a do-while loop. 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 (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.
Upvotes: 5