'Need help calculating the amount of time spent traveling
I am having trouble calculating the time spent traveling. I attached some images one is what I'm trying to get and the other is what I am getting. I find it difficult to calculate the time because of the change of speed. Is there any recommendations of how i should go about calculating the estimated time traveled.
public class YourCarProblem {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
int cont;
int gas = 0;
double miles=0;
int mph=0;
int counter = 1;
int con = 0;
int total=0;
int totalmph=0;
double time=0;
System.out.print("Are you going on a trip(1=yes or 0=no)?: ");
cont = input.nextInt();
if (cont==1) {
System.out.print("How many gallons of gas in you tank(Integer 1-20):");
gas = input.nextInt();
gas+=gas;
System.out.print("Enter leg distance(Miles)");
miles=input.nextDouble();
total+=miles;
System.out.print("Enter leg 1 speed(MPH)");
mph=input.nextInt();
totalmph+=mph;
System.out.print("Is there another leg to your trip(1=yes or 0=no)");
con=input.nextInt();
counter++;
}
else {
System.out.println("Thank you have a good day");
}
while(con>0) {
if (con==1) {
System.out.printf("Enter leg %d distance(Miles): ",counter);
miles=input.nextDouble();
total+=miles;
System.out.printf("Enter leg %d Speed(MPH): ",counter);
mph=input.nextInt();
counter++;
System.out.print("Is there another leg to your trip(1=yes or 0=no)");
con=input.nextInt();
}
else if(con==0) {
System.out.print("have a good day");
}
}
time = total/mph;
System.out.printf("You traveled about %d miles in about %f",total,time);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
