Java question... i am setting up a program that inputs 2 user data through input dialog boxes from JOptionPane. i am using a sentinel value control that indicates when the user enters in a sentinel value set to -1, it will calculate the data inputted. this is what it looks like so far..
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
//beginning class
public class Gas {
//beginning method
public static void main(String args[])
{
//declaring variables
int miles, gallons, totalmiles, totalgallons, counter;
double averagemiles, totalaverage;
String usermiles, gallonmiles, result;
//initialization
totalmiles = 0;
totalgallons = 0;
counter = 0;
averagemiles=0;
totalaverage=0;
//obtaining user inputs
usermiles = JOptionPane.showInputDialog("Enter Miles Driven or -1 to quit");
gallonmiles = JOptionPane.showInputDialog("Enter Gallons Used");
//parseing from string to integers
miles = Integer.parseInt(usermiles);
gallons = Integer.parseInt(gallonmiles);
//loop until sentinel value read from user
while (miles != -1){
//getting next user input
usermiles = JOptionPane.showInputDialog("Enter Miles Driven or -1 to quit");
gallonmiles = JOptionPane.showInputDialog("Enter Gallons Used");
miles = Integer.parseInt(usermiles); //parseing from string to integers
gallons = Integer.parseInt(gallonmiles);
totalmiles = totalmiles + miles;
totalgallons = totalgallons + gallons;
counter = counter + 1;
//end while
}
//termination phase
DecimalFormat twoDigits = new DecimalFormat("0.00");
if (counter != 0){
// calculate the averages
averagemiles = (double)miles / (double)gallons;
totalaverage = totalmiles / totalgallons;
}
//diplaying result
result = ("Average Miles/Gallon : " + averagemiles
+ "\nTotal Miles/Gallon : " + totalaverage);
JOptionPane.showMessageDialog(null, result, "Mileage", JOptionPane.INFORMATION_MESSAGE);
//terminate program
System.exit(0);
//ending method main
}
//ending class
}
for some reason, when i enter -1 to stop the second while loop, it still asks for the gallons used input box. it should by pass the whole while loop when the user enters sentinel value -1.
anyone have any ideas.. |