Search Blog

Biyernes, Enero 18, 2013

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.


Java Codes

--+o0O0o+--

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.


Code:


import java.util.Scanner;

public class MachineProblem7 {

    public static void main(String[] args) {
       
        Scanner input_user = new Scanner(System.in);
       
        float gallons,miles;
        float computed,overall = 0,result = 0,p = 0;
       
        System.out.println("*****************************************");
    
 for(int x = 0;x < 1000;x++)
      { 
           
       System.out.print(" Enter the galloon used (-1 to end): ");
            gallons = input_user.nextFloat();
           
            if(gallons != -1)
            {
                System.out.print(" Enter the miles driven: ");
                miles = input_user.nextFloat();
               
                computed = miles / gallons;
                overall = overall + computed;

System.out.println("The miles / gallon for this tank was " + computed);
                p++;
                System.out.println("*****************************************");
            }
            if(gallons == -1)
            {
                result = overall / p;
                System.out.println("*****************************************");

System.out.println("   The overall average miles / gallon was " + result);
                System.out.println("*****************************************");
                return;
            }        
            
        }
    }
}

Walang komento:

Mag-post ng isang Komento