| | |
cant get the final sum
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
import javax.swing.JOptionPane;
public class cafe2
{
public static void main(String args[])
{
String name,ch;
double price = 0.00;
double sum=0.00;
int cho=0;
name=JOptionPane.showInputDialog("Enter
Customer Name:");
do{
ch=JOptionPane.showInputDialog(
" Food Name "+
"\n-----------------------------------------------------"+
"\n1) French Fries $ 3.50"+
"\n2) Fish and Chips $ 5.50"+
"\n3) Goat Cheese Pizza $20.70"+
"\n4) Chicken Salad $ 8.90"+
"\n5) Spaghetti $ 9.00"+
"\n6) Chicken Chop $ 9.50"+
"\n7) Lamb Chop $10.50"+
"\n\n Beverages "+
"\n-----------------------------------------------------"+
"\n8) Ice Blended Mocha $ 5.60"+
"\n9) Cafe Classic Martini $13.00"+
"\n0) EXIT"+
"\n\n PLEASE Enter Item No. :");
cho=Integer.parseInt (ch);
switch (cho) {
case 1: price= 3.50;
break;
case 2: price= 5.50;
break;
case 3: price= 20.70;
break;
case 4: price= 8.90;
break;
case 5: price= 9.00;
break;
case 6: price= 9.50;
break;
case 7: price= 10.50;
break;
case 8: price= 5.60;
break;
case 9: price= 10.50; break;
default: System.out.println("Invalid
choice.");
}
}
while(cho!=0);
sum = sum + price;
JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: $"+sum);
System.exit(0);
}
}
//*** PROBLEM IS I CANT GET THE TOTAL IN
//THE END
public class cafe2
{
public static void main(String args[])
{
String name,ch;
double price = 0.00;
double sum=0.00;
int cho=0;
name=JOptionPane.showInputDialog("Enter
Customer Name:");
do{
ch=JOptionPane.showInputDialog(
" Food Name "+
"\n-----------------------------------------------------"+
"\n1) French Fries $ 3.50"+
"\n2) Fish and Chips $ 5.50"+
"\n3) Goat Cheese Pizza $20.70"+
"\n4) Chicken Salad $ 8.90"+
"\n5) Spaghetti $ 9.00"+
"\n6) Chicken Chop $ 9.50"+
"\n7) Lamb Chop $10.50"+
"\n\n Beverages "+
"\n-----------------------------------------------------"+
"\n8) Ice Blended Mocha $ 5.60"+
"\n9) Cafe Classic Martini $13.00"+
"\n0) EXIT"+
"\n\n PLEASE Enter Item No. :");
cho=Integer.parseInt (ch);
switch (cho) {
case 1: price= 3.50;
break;
case 2: price= 5.50;
break;
case 3: price= 20.70;
break;
case 4: price= 8.90;
break;
case 5: price= 9.00;
break;
case 6: price= 9.50;
break;
case 7: price= 10.50;
break;
case 8: price= 5.60;
break;
case 9: price= 10.50; break;
default: System.out.println("Invalid
choice.");
}
}
while(cho!=0);
sum = sum + price;
JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: $"+sum);
System.exit(0);
}
}
//*** PROBLEM IS I CANT GET THE TOTAL IN
//THE END
WELLLL, one of your major problems in your code is that you have an infinite loop.
Translation --- Your telling your code to add price to your sum until cho equals zero, which it never does because you never update your condition.
The correct way to use a loop:
Translation --- Your telling your code to add price to your sum until cho equals zero, which it never does because you never update your condition.
The correct way to use a loop:
Java Syntax (Toggle Plain Text)
while(choice != 0) { sum = sum + price; choice = choice - 1; // or choice--; }
Doing what they say couldn't be done!!!!
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
WELLLL, one of your major problems in your code is that you have an infinite loop.
Translation --- Your telling your code to add price to your sum until cho equals zero, which it never does because you never update your condition.
The correct way to use a loop:
Java Syntax (Toggle Plain Text)
while(choice != 0) { sum = sum + price; choice = choice - 1; // or choice--; }
while(cho != 0);
{
sum = sum + price;
cho = cho --; // or choice--;
}
JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: RM"+sum);
and
while(cho != 0);
{
sum = sum + price;
cho = cho --; // or choice--;
JOptionPane.showMessageDialog(null,
"RECEIPT"+
"\n*********"+
"\nCustomer Name: "+name+
"\n\nTotal Price: RM"+sum);
}
and i still get the same thing...wierd..have been trying to figure it out for almost a week tho
Correct me if im wrong, but for what your trying to do I don't think you need a while loop.
You want to add up the total amount of money that was choosen from each menu choice.
All you need is to do is check one condition, is choice = 0. If not then do this, if yes then continue.
Another thing when your writing code you want to try and make your code readable and effiecent. You have a couple of options here, add that statement that's in the while loop in your switch body after each case, or use if-else statments which would probably be more elegent. I think you need to redesign your program, using the most effiecent coding style. One more thing I will point out.
On your menu options you have a choice of 0 which exits the program but in your switch body you dont take that into account, so when your program runs your always going to get the message , everytime a user choose to exit the program. You dont need choice = choice-- , the two dashes after a variable (choice--) is equivalent to choice = choice - 1, so use one or the other.
You want to add up the total amount of money that was choosen from each menu choice.
All you need is to do is check one condition, is choice = 0. If not then do this, if yes then continue.
Another thing when your writing code you want to try and make your code readable and effiecent. You have a couple of options here, add that statement that's in the while loop in your switch body after each case, or use if-else statments which would probably be more elegent. I think you need to redesign your program, using the most effiecent coding style. One more thing I will point out.
On your menu options you have a choice of 0 which exits the program but in your switch body you dont take that into account, so when your program runs your always going to get the message
Java Syntax (Toggle Plain Text)
default: System.out.println("Invalidchoice.");
•
•
•
•
switch (cho) {
case 1: price= 3.50;
break;
case 2: price= 5.50;
break;
case 3: price= 20.70;
break;
case 4: price= 8.90;
break;
case 5: price= 9.00;
break;
case 6: price= 9.50;
break;
case 7: price= 10.50;
break;
case 8: price= 5.60;
break;
case 9: price= 10.50; break;
default: System.out.println("Invalid
choice.");
}
Last edited by WhYuLoOkIn; Feb 10th, 2008 at 8:16 am.
Doing what they say couldn't be done!!!!
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
i still have the code...erm,move it where? i moved it after the switch and after the while..but no change... an example would help..
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
hi guys..thanks alot for the help..at last i got if figured out..might look messy,but it works... yup..its the sum line. here's my new line:
case 1: price= 3.50;
sum = sum + price;
break;
case 2: price= 5.50;
sum = sum + price;
break;
case 3: price= 20.70;
sum = sum + price;
break;
case 4: price= 8.90;
sum = sum + price;
break;
case 5: price= 9.00;
sum = sum + price;
break;
now to figure out how to get 2 decimal pts
case 1: price= 3.50;
sum = sum + price;
break;
case 2: price= 5.50;
sum = sum + price;
break;
case 3: price= 20.70;
sum = sum + price;
break;
case 4: price= 8.90;
sum = sum + price;
break;
case 5: price= 9.00;
sum = sum + price;
break;
now to figure out how to get 2 decimal pts
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
thanks for the correction... typo mistake there.. yup i got 48.5 as my output
i decided to remove my default message, assuming the user knows wht they are doing.
case 9: price= 13.00;
sum = sum + price;
break;
case 0:
JOptionPane.showMessageDialog(null,"Thank You");
break;
i decided to remove my default message, assuming the user knows wht they are doing.
case 9: price= 13.00;
sum = sum + price;
break;
case 0:
JOptionPane.showMessageDialog(null,"Thank You");
break;
Last edited by ndrux; Feb 11th, 2008 at 1:05 am.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Need Ideas For Dissertations, Final Year Degree Project (IT Professionals' Lounge)
- Number crunching (Python)
- Help on Emu8086 calculating Sum (Assembly)
- Splitting a string? (C)
- I Need Help Please Writing This Program (Python)
- need help on final year project (Java)
Other Threads in the Java Forum
- Previous Thread: nested for loop problem
- Next Thread: BOM Problem with crimson Parser
Views: 1142 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet application arguments array arrays binary bluetooth c++ chat class classes client code compiler component converter coordinates data database db desktop detection eclipse error event exception fast file filei/o fractal givemetehcodez graphics gui helpwithhomework html ide image inheritance input integer j2me java jframe jni jpanel jtable linked linked-list list loop looping method mobile netbeans newbie number object objects oracle os page pattern phone pixel print problem program programming project read regex remove return robot scanner server service set size sms software sort source sql string swing test text text-file threads timer translate tree url user web





