Question
Miles per gallon with arrays: Declare arrays User prompt -> get the size of the arrays (how many weeks) and then create the arrays User
Miles per gallon with arrays: Declare arrays
User prompt -> get the size of the arrays (how many weeks) and then create the arrays
User prompt -> get the values for the Miles array.
User prompt -> get the values for the Gallons array.
Calculate values for the MPG Array.
Display the three arrays as so:
Miles Gallons MPG ------ -------- --------- Week 1 240.5 10.3 23.3 Week 2 300.0 15.6 19.2 Total 540.5 25.9 20.8 Code so far (calculateMpg is broken):
package milespergallon;
import java.util.Arrays; import java.util.Scanner;
/** * * @author x */ public class MilesPerGallon {
private static int[] result;
public static int[] calculateMpg(int[] miles, int[] gallons) { for (int i = 0; i < miles.length; i++) { for (int j = 0; j < gallons.length; j++) { int result; result = miles[i] / gallons[j]; } } return result; }
/** * @param args the command line arguments */ public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int[] miles; int[] gallons; int[] mpg; int numOfWeeks;
System.out.println("How many weeks did you track? "); numOfWeeks = stdin.nextInt();
miles = new int[numOfWeeks]; gallons = new int[numOfWeeks];
for (int i = 0; i < numOfWeeks; i++) { System.out.println("How many miles did you drive in week " + (i + 1)); miles[i] = stdin.nextInt(); System.out.println("How many gallons did you use in week " + (i + 1)); gallons[i] = stdin.nextInt(); }
mpg = calculateMpg(miles, gallons);
System.out.println("\tMiles\tGallons\tMPG"); System.out.println("\t-----\t-------\t---"); System.out.println("Week 1:\t" + miles[0] + "\t" + gallons[0] + "\t" + mpg[0]); System.out.println("Week 2:\t" + miles[1] + "\t" + gallons[1] + "\t" + mpg[1]);
}
}
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