-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasMileage.java
More file actions
37 lines (29 loc) · 1.07 KB
/
GasMileage.java
File metadata and controls
37 lines (29 loc) · 1.07 KB
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
31
32
33
34
35
36
37
import java.util.Scanner;
public class GasMileage {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int miles;
int gallons;
double tripMPG;
int totalMiles = 0;
int totalGallons = 0;
System.out.print("Enter miles driven (-1 to quit): ");
miles = sc.nextInt();
while (miles != -1) {
System.out.print("Enter gallons used: ");
gallons = sc.nextInt();
tripMPG = (double) miles / gallons;
System.out.println("Miles per gallon for this trip: " + tripMPG);
totalMiles += miles;
totalGallons += gallons;
System.out.print("\nEnter miles driven (-1 to quit): ");
miles = sc.nextInt();
}
if (totalGallons != 0) {
double combinedMPG = (double) totalMiles / totalGallons;
System.out.println("\nCombined miles per gallon: " + combinedMPG);
} else {
System.out.println("No data entered.");
}
}
}