When I run this, it will prompt for miles and then will get hung up and not progress to ask for mph. It is supposed to go back into the loop and if 0 is entered for miles, it will end the program. Why is it getting hung up after my while?
import java.text.NumberFormat;
import java.util.Scanner;
// use integer division to divide time into hours and minutes?
public class TravelTimeApp
{
private static double miles;
private static double milesPerHour;
private static double hours;
private static int minutes;
private static int hoursInt;
public TravelTimeApp ()
{
miles = 0;
}
public static void main(String[] args)
{
//NumberFormat
NumberFormat onePlace = NumberFormat.getNumberInstance();
onePlace.setMaximumFractionDigits(1);
// create the Scanner object
Scanner sc = new Scanner(System.in);
System.out.printf(" Enter miles traveled, or a zero (0) to exit -> ");
miles = sc.nextDouble();
if (miles == 0)
{
System.out.printf("\n Program completed.");
System.exit(0);
}
while(miles != 0)
{
System.out.printf(" Enter miles per hour rate of travel -> ");
milesPerHour = sc.nextDouble();
// calculate the travel time in hours with decimal division
hours = miles / milesPerHour;
// get number of minutes as an int
minutes = (int) (hours * 60);
// use integer arithmetic to get hours and minutes as int values
hoursInt = minutes / 60;
minutes = minutes % 60;
// display the result
System.out.printf(" \nEstimated Travel Time (%,.1f miles at %,.1f mph)\n", miles,milesPerHour);
System.out.printf(" Hours%,10d\n", hoursInt);
System.out.printf(" Minutes%,8d\n\n", minutes);
}
}
}
milesin the loop?milesat some point in the loop. Otherwise, the loop's condition will remaintrueforever.