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");
}
}
Re: Help with my program please :) I can't figure this out.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
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.
Re: Help with my program please :) I can't figure this out.
Thanks for your reply. I appreciate the tip
Re: Help with my program please :) I can't figure this out.
The formatting of your code with all the statements starting in the first column makes it hard to understand the logic.
All code within a pair of {}s should be indented 3-4 spaces to make it easy to see the nesting of the logic.
Continued lines should be indented so the continued part is obvious.