Results 1 to 2 of 2
- 01-10-2009, 02:06 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
Repeat while answer yes - do while loop
Can someone help, it seems like either i type in "y" or "n", the program won't go back to main menu...
also compile error:
variable itemPrice might not have been initialized.
Should i change to while loop instead? I'm new to while and do-while loop.. Please advice.
import java.text.*; // to use DecimalFormat
import java.util.*; // to use Scanner class
import javax.swing.*; // In order to use JOptionPane
public class ModOrderSystemA
{
public static final double TAX_AMOUNT = 0.50;
public static void main(String[] args)
{
String choice, itemName, input, total, answer= "Y";
int quantity;
double itemPrice, priceBeforeTax, priceAfterTax;
boolean anyMore = true;
Scanner keyboard = new Scanner (System.in);
//Display the food menu
do
{
choice =
JOptionPane.showInputDialog("Welcome to the Restruant!\n" +
"Here is today's special: \n" +
"A. Rice $1.50\n" +
"B. Noddle $2.00\n" +
"C. Soup $2.50\n" +
"Please enter your order(A,B,C:");
choice = choice.toUpperCase();
if (! choice.equals ("A") &&
! choice.equals ("B") &&
! choice.equals ("C"))
{
JOptionPane.showMessageDialog (null,
"We don't have choice " + choice + " in our menu.\n" +
"The order has to be A,B,C,D,E,F or G");
}
//else if(choice.equals ("A") &&
// choice.equals ("B") &&
// choice.equals ("C"))
else
{
input =
JOptionPane.showInputDialog("How many would you like to order:");
quantity = Integer.parseInt(input);
if (quantity <= 0)
{
JOptionPane.showMessageDialog (null,
"Error: Invalid quantity.\n");
}
else
{
// Display name of the Food
// Set price of the item
if (choice.equals ("A"))
{
itemName = " Rice";
itemPrice = 1.50;
}
else if (choice.equals ("B"))
{
itemName = " Noddle";
itemPrice = 2.00;
}
else
{
itemName = " Soup";
itemPrice = 2.50;
}
JOptionPane.showMessageDialog (null,
" You ordered " + quantity + itemName);
answer = JOptionPane.showInputDialog("Would you like another order?\n" +
"Please enter Y or y for another order.\n" +
"Enter N or n to exit");
answer = keyboard.next ();
}
}
if(!answer.equalsIgnoreCase("y"))
{
anyMore = false;
priceBeforeTax = itemPrice * quantity;
priceAfterTax = priceBeforeTax + TAX_AMOUNT;
DecimalFormat oneAfter = new DecimalFormat ("0.00");
JOptionPane.showMessageDialog(null,
"Here is the total price for your orders: \n" +
"Price before tax is $" + oneAfter.format(priceBeforeTax) +
"\nTax is $" + TAX_AMOUNT +
"\nPrice after tax is $" + oneAfter.format(priceAfterTax) +
"\n\nThank you for your order and See you again!");
}
}while(answer.equalsIgnoreCase("y"));
}
}Last edited by AJ2009; 01-10-2009 at 02:14 PM.
- 01-10-2009, 06:19 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What are you doing in this lone of code.
Once you get an input from the user, VM machine wait to see another input on command line. Until you enter the value the rest of the code wont execute. Remove that line and see.Java Code:answer = keyboard.next ();
And also please use code tags when you posting again in the forum. Unformated codes are really hard to read.
Similar Threads
-
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
i want my answer to a whole number or i think an int, please help?
By soc86 in forum New To JavaReplies: 3Last Post: 11-02-2008, 01:29 AM -
Plz answer this question ...
By raghu2114 in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 06:36 PM -
java.util.zip.ZipException: invalid bit length repeat EXCEPTION
By hemanthjava in forum Advanced JavaReplies: 1Last Post: 07-02-2008, 02:15 PM -
nested <ui:repeat> - problem
By hackerofcrackers in forum JavaServer Faces (JSF)Replies: 0Last Post: 06-20-2008, 10:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks