-5

Here's the task at hand, why it really matters to have the user enter the number of trips in a for loop is beyond me, seems like wasted code. Any help would be greatly appreciated:

Write a Java program that calculates a user’s gas mileage. Ask the user how many trips he/she took in his/her car. Write a For Loop that asks the user two questions for each trip: how many miles the trip was (as a decimal number), and how many gallons of gas were used (as a decimal number). After the user has entered this information for each trip, calculate his/her total gas mileage and print it to the screen. The gas mileage can be calculated by dividing the total miles driven by the total number of gallons of gas used.

1 Answers1

1

Maybe I'm wrong, but it seems that you are asking for someone else to do your homework.If you want to get some effective help from the community you must show us a little effort in doing the homework yourself. I assume that you are unable to start with your assignment so I give you some starting points:

  1. You can use a scanner to read the user input from the console; you can see from here an example of reading user input; please take also a look at the java api documentation (available here)
  2. You must decide if you want to read a String from the input and converting manually to a decimal number or if to read a decimal numer form the input; if you are learning java I suggest you to start using scanner.nextDouble and when the program rusn correctly to change it and tryi to parse the user input manually and giving the correct feedback if what the user entered is not a number
  3. your program should read an integer first (call it tripsNo for example) and then loop tripsNo times asking for each trip data

If you try to arrange the program by yourself using the prevoius points, then show us what you have done, I'm quite sure you'll get some answer on the problems you are facing with the code.

Community
  • 1
  • 1
Giovanni
  • 3,561
  • 1
  • 19
  • 25