-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasMileage.java
More file actions
31 lines (26 loc) · 918 Bytes
/
Copy pathGasMileage.java
File metadata and controls
31 lines (26 loc) · 918 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
31
import java.util.Scanner;
public class GasMileage {
public static void main(String... args) {
Scanner input = new Scanner(System.in);
int totalMiles = 0;
int totalGallons = 0;
while (true) {
System.out.print("Enter the miles for the trip (or -1 to end): ");
int miles = input.nextInt();
if (miles == -1) {
break;
}
totalMiles += miles;
System.out.print("Enter the gallons for the trip: ");
int gallons = input.nextInt();
totalGallons += gallons;
}
if (totalGallons != 0) {
float totalAverage = (float) totalMiles / totalGallons;
System.out.printf("The average miles per gallon is: %.2f", totalAverage);
}
else {
System.out.println("No gallons entered, cannot calculate average.");
}
}
}