Advertisement
vp0415

lec3.1

Oct 21st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GasMileage {
  4.     public void calculateGasMileage(){
  5.         Scanner input = new Scanner(System.in);
  6.        
  7.        
  8.         int tripsCount = 1;
  9.         int totalMiles = 0;
  10.         int totalGallons = 0;
  11.         double avgMilesPerGallon = 0.0;
  12.        
  13.         while(true) {
  14.             System.out.println("Enter miles or -1 to quit");
  15.             int miles = input.nextInt();
  16.             if(miles == -1)
  17.                 break;
  18.             System.out.println("Enter gallons");
  19.             int gallons = input.nextInt();
  20.             double milesPerGallon = miles/(double)gallons;
  21.             System.out.println("Trip " + tripsCount + " used " + milesPerGallon + " miles per gallon" );
  22.            
  23.             totalMiles = totalMiles + miles;
  24.             totalGallons = totalGallons + gallons;
  25.            
  26.             avgMilesPerGallon = avgMilesPerGallon + totalMiles/(double)totalGallons;
  27.             System.out.println("Miles per gallon for now " + avgMilesPerGallon );
  28.            
  29.             tripsCount++;
  30.         }
  31.        
  32.        
  33.         System.out.println("Miles per gallon for all trips " + avgMilesPerGallon );
  34.        
  35.        
  36.     }
  37.     }
  38.  
  39. public class GasMileageTest {
  40.  
  41.     public static void main(String[] args) {
  42.         GasMileage trips = new GasMileage();
  43.        
  44.         trips.calculateGasMileage();
  45.  
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement