-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilesPerGallon.java
More file actions
30 lines (25 loc) · 903 Bytes
/
Copy pathMilesPerGallon.java
File metadata and controls
30 lines (25 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* MilesPerGallon.java
* Purpose: Computes the mpg given start and end odometer readings, and the gallons used.
*/
import java.util.*;
import java.text.DecimalFormat;
public class MilesPerGallon
{
// Computes the miles per gallon achieved given the gallons used and the odometer readings.
public static void main (String[] args)
{
double gas, mpg;
long odometer1, odometer2;
Scanner keyboard = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat ("0.##");
System.out.print ("Enter the gas used: ");
gas = keyboard.nextDouble();
System.out.print ("Initial odometer reading: ");
odometer1 = keyboard.nextLong();
System.out.print ("Final odometer reading: ");
odometer2 = keyboard.nextLong();
mpg = (odometer2 - odometer1) / gas;
System.out.println ("Miles Per Gallon: " + fmt.format(mpg));
}
}