Hi Im trying to write simple miles per gallon program, I have gotten every part so far, but I cannot figure out how to "calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point."
package random;
import java.util.Scanner;
public class random17 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
while (true) {
System.out.print("Enter Miles Driven: ");
float milesDriven = input.nextFloat();
if (milesDriven < 0) {
break;
}
System.out.print("Enter Gallons Used: ");
float gallonsUsed = input.nextFloat();
if (gallonsUsed < 0) {
System.out.println("*Invalid Input*");
break;
}
double mpg = (double)milesDriven / (double)gallonsUsed;
System.out.print("Miles Per Gallon: " + mpg + "\n");
float count = gallonsUsed/milesDriven;
float total = 0;
total += (float)mpg;
// test code //System.out.println(total);
float avgmpg = (float)total / count;
System.out.printf("Average MPG: " + avgmpg + "\n");
System.out.println("--------------------------------");
}
}
}