- Create a variable container named miles which can hold integers.
- Create a variable container named gallons which can hold decimal numbers.
- Create a variable container named milesPerGallon which can hold decimal numbers.
- Ask the user for the number of miles and store their answer in miles.
- Ask the user for the number of gallons of gasoline purchased and store their answer in gallons.
- Divide the value in miles by the value in gallons and store the result in milesPerGallon.
- Output the value in the following containers and literals:
- “Your mileage is “
- milesPerGallon
- "."
|
1 import java.util.Scanner; // make Scanner available to the computer
2
3 /** Calculate Miles Per Gallon
4 *
5 * @author Nancy Harris
6 * @version V1 - 09/02/2011
7 ******************************************************/
8 public class MPG
9 {
10 public static void main (String args[])
11 {
12 // declare the containers
13 int miles;
14 double gallons;
15 double milesPerGallon;
16
17 // get the input
18
19 // prepare the input device
20 Scanner keyboard;
21 keyboard = new Scanner(System.in); // input is standard input
22
23 // prompt the user and get the values
24 System.out.print("Enter the miles: ");
25 miles = keyboard.nextInt();
26
27 System.out.print("Enter the gallons: ");
28 gallons = keyboard.nextDouble();
29
30 // manipulate the data
31 milesPerGallon = miles / gallons;
32
33 // output the result
34 System.out.print("Your mileage is ");
35 System.out.print(milesPerGallon);
36 System.out.println(".");
37 }
38 } |