Advertisement
binibiningtinamoran

GasMileage.java

Jun 3rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GasMileage {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         System.out.print("Enter the distance in miles: ");
  9.         double distance = scan.nextDouble();
  10.         while (distance < 0) {
  11.             System.out.print("Negative distance is not allowed. Enter again: ");
  12.             distance = scan.nextDouble();
  13.             System.out.println();
  14.         }
  15.  
  16.         System.out.print("Enter the gas in gallons: ");
  17.         double gallons = scan.nextDouble();
  18.         while (gallons < 0) {
  19.             System.out.print("Negative gallons is not allowed. Enter again: ");
  20.             gallons = scan.nextDouble();
  21.             System.out.println();
  22.         }
  23.  
  24.         System.out.printf("The number of gallons of gas consumed by the car is %,.2f miles per gallon", distance/gallons);
  25.  
  26.     }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement