Okay, I am so close to this one I can taste it. I am trying to get the average mileage between two different tanks of gas in this code. I have most of the code written out, but I know there is something missing, so here we go!

import java.util.Scanner;
public class Main {

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        int totalMiles = 0;
        int totalGallons = 0;
        int totalMiles2 = 0;
        int totalGallons2 = 0;
        int avgMiles = 0;
        int avgMiles2 = 0;
        int avgMiles3 = 0;
        Scanner input = new Scanner(System.in);
        while (totalMiles != -1)
            System.out.print("How many miles on first trip: ");
            totalMiles = input.nextInt();
            if (totalMiles != -1)
                System.out.print("How Many Gallons in first tank: ");
                totalGallons = input.nextInt();
             avgMiles = (totalMiles / totalGallons);
                System.out.printf("Your average mileage is: %d\n",avgMiles);
             System.out.print("How many miles on second trip: ");
            totalMiles2 = input.nextInt();
                System.out.print("How Many Gallons in second tank: ");
                totalGallons2 = input.nextInt();
             avgMiles = (totalMiles2 / totalGallons2);
                System.out.printf("Your average mileage is: %d\n",avgMiles2);

             avgMiles3 = (avgMiles + avgMiles2);
                System.out.printf("Your total average miles: %d\n",avgMiles3);
6 Years
Discussion Span
Last Post by adarshcu

What is the error that you are getting.? Also there may be a logical mistake as average of any two quantities should be divided by two.

avgMiles3 = (avgMiles + avgMiles2)/2

Isn't it .?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.