Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GasMileage {
- public void calculateGasMileage(){
- Scanner input = new Scanner(System.in);
- int tripsCount = 1;
- int totalMiles = 0;
- int totalGallons = 0;
- double avgMilesPerGallon = 0.0;
- while(true) {
- System.out.println("Enter miles or -1 to quit");
- int miles = input.nextInt();
- if(miles == -1)
- break;
- System.out.println("Enter gallons");
- int gallons = input.nextInt();
- double milesPerGallon = miles/(double)gallons;
- System.out.println("Trip " + tripsCount + " used " + milesPerGallon + " miles per gallon" );
- totalMiles = totalMiles + miles;
- totalGallons = totalGallons + gallons;
- avgMilesPerGallon = avgMilesPerGallon + totalMiles/(double)totalGallons;
- System.out.println("Miles per gallon for now " + avgMilesPerGallon );
- tripsCount++;
- }
- System.out.println("Miles per gallon for all trips " + avgMilesPerGallon );
- }
- }
- public class GasMileageTest {
- public static void main(String[] args) {
- GasMileage trips = new GasMileage();
- trips.calculateGasMileage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement