Help with my program please :) I can't figure this out.

• February 21st, 2012, 06:43 PM
scholaryoshi
Help with my program please :) I can't figure this out.
Java program is not calculating total?
I have to write a program that prompts the user to make a choice of food then adds the total. The user needs to prompted 3 times.
When I run the program it only gives me a total of zero. Why is it not calculating the total?
Code java:

```import javax.swing.JOptionPane; public class FastFood { double total = 0; public static void main(String[] args) {   double choice = getChoice(); if (choice == 0) JOptionPane.showMessageDialog(null, "Your total is \$0.00."); else { calculateTotal(total, choice); choice = getChoice(); if (choice == 0) JOptionPane.showMessageDialog(null, "Your Total is:" + total ); else { calculateTotal(total, choice); choice = getChoice(); if (choice == 0) JOptionPane.showMessageDialog(null, "Your Total is:" + total ); else { calculateTotal(total, choice); JOptionPane.showMessageDialog(null, "Your Total is:" + total ); } } } }   public static double getChoice() { String input = JOptionPane.showInputDialog(null, "Choose your meal Item" + "\n(1)Burger \$4.99" + "\n(2)Pepsi \$2.00" + "\n(3)Chips \$0.75"); return Double.parseDouble(input); }   public static void calculateTotal(double total, double choice) { if (choice == 1) total = total + 4.99; else if (choice == 2) total = total + 2.00; else if (choice == 3) total = total + 0.75; else JOptionPane.showMessageDialog(null, "Invalid choice enter numbers 0-3 only"); } }```
• February 22nd, 2012, 07:58 AM
Norm
Re: Help with my program please :) I can't figure this out.

Why are you reading in the user's choice as a double and not as an int?

To debug your code, add lots of printlns to the code to print out the values of ALL the variables as the code executes to show you what the program is doing as it executes. Be sure to add ID labels to all the printouts so you know what was printed.
• February 24th, 2012, 01:01 AM
scholaryoshi
Re: Help with my program please :) I can't figure this out.