Question
This program gets the number of miles traveled and the gallons of gas used, and prints out MPG. You are to complete the method AND
This program gets the number of miles traveled and the gallons of gas used, and prints out MPG. You are to complete the method AND add a loop to main so that you continually ask for input and calculate MPG until the user gives you a zero for the miles traveled. NOTE: Do not ask for gallons of gas or call the method if miles is zero. I suggest you FIRST write the method and then worry about the loop.
import java.util.Scanner;
public class Number1 {
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //get the miles traveled System.out.println("Enter the number of miles traveled, 0 to stop"); double miles=keyboard.nextDouble(); System.out.println("Enter the amount of gas consumed"); //get the gas used double gas=keyboard.nextDouble(); //output MPG double mpg=MilesPerGallon(miles, gas); System.out.println("Gas usage was " + mpg + " MPG"); } public static double MilesPerGallon(double m, double g) { //calculates mpg by dividing miles by gallons //and returns the result }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started