Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GasMileage {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter the distance in miles: ");
- double distance = scan.nextDouble();
- while (distance < 0) {
- System.out.print("Negative distance is not allowed. Enter again: ");
- distance = scan.nextDouble();
- System.out.println();
- }
- System.out.print("Enter the gas in gallons: ");
- double gallons = scan.nextDouble();
- while (gallons < 0) {
- System.out.print("Negative gallons is not allowed. Enter again: ");
- gallons = scan.nextDouble();
- System.out.println();
- }
- System.out.printf("The number of gallons of gas consumed by the car is %,.2f miles per gallon", distance/gallons);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement