4. Class Name: MileAge. Translate the algorithm below into Java. Don’t forget to declare variables before they are used.
a. Print a line indicating this program will calculate mileage
b. Print prompt to user asking for miles driven
c. Read in miles driven
d. Print prompt to user asking for gallons used
e. Read in gallons used
f. Calculate miles per gallon by dividing miles driven by gallons used
g. Print miles per gallon along with appropriate labels
import java.util.Scanner;
public class MileAge {
public static void main(String[] args) {
Scanner amante = new Scanner(System.in);
double miles, mpg, gallons;
System.out.print("How many miles were driven?");
miles = amante.nextDouble();
System.out.print("How many gallons were used?");
gallons = amante.nextDouble();
mpg = miles/gallons;
System.out.println(" The Miles per Gallon used in the trip are: "+mpg);
}
}
Walang komento:
Mag-post ng isang Komento