Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9225c69

Browse files
Shows use of Scanner class to read numeric data
Calculates fuel efficiency based on values entered by the user.
1 parent 97f4254 commit 9225c69

File tree

1 file changed

+24
-0
lines changed
  • textbook work/java software solutions/2. Data and Expressions

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//********************************************************************
2+
// GasMileage.java Author: Lewis/Loftus
3+
//
4+
// Demonstrates the use of the Scanner class to read numeric data.
5+
//********************************************************************
6+
import java.util.Scanner;
7+
8+
public class GasMileage {
9+
//-----------------------------------------------------------------
10+
// Calculates fuel efficiency based on values entered by the
11+
// user.
12+
//-----------------------------------------------------------------
13+
public static void main(String[] args){
14+
int miles;
15+
double gallons, mpg;
16+
Scanner scan = new Scanner(System.in);
17+
System.out.print("Enter the number of miles: ");
18+
miles = scan.nextInt();
19+
System.out.print("Enter the gallons of fuel used: ");
20+
gallons = scan.nextDouble();
21+
mpg = miles / gallons;
22+
System.out.println("Miles Per Gallon: " + mpg);
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /