# Programming exercise help

• 01-13-2012, 03:07 AM
atac57
Programming exercise help
Quote:

Write a program that calculates miles per gallon for a list of cars. The data for each car consists of initial odometer reading, final odometer reading, and number of gallons of gas. The user signals that there are no more cars by entering a negative initial odometer reading.

Miles Per Gallon Program
Initial miles:
15000
Final miles:
15250
Gallons
10
Miles per Gallon: 25.0

Initial miles:
107000
Final miles:
107450
Gallons
15
Miles per Gallon: 30.0

Initial miles:
-1
bye
I'm having a couple problems with this exercise. How do you initialize the "first" variable when its suppose to be dependent on the user input which is what determines if the while loop executes (a negative number is suppose to stop the loop). If you initialize it before the loop with the Scanner variable, its gonna pointlessly have you enter the value after the "Miles Per Gallon Program" prompt? How do you get around that or am I completely off track? Thanks.

Code:

```import java.util.Scanner; class Simulation{         public static void main (String args[]){                 Scanner scan = new Scanner(System.in);                                 int first, last, gallons, milesGallon;                                 System.out.println("Miles Per Gallon Program");                                 while ( first >= 0)                 {                         System.out.println("Initial miles: ");                         first = scan.nextInt();                                                 System.out.println("Final miles: ");                         last = scan.nextInt();                                                 System.out.println("Gallons: ");                         gallons = scan.nextInt();                                                 milesGallon = (last - first)/ gallons;                         System.out.println("Miles per Gallon: " + milesGallon);                 }         } }```
• 01-13-2012, 06:09 AM
Re: Programming exercise help
Quote:

How do you initialize the "first" variable when its suppose to be dependent on the user input which is what determines if the while loop executes
Ever hear of a do-while loop? Look it up.
• 01-13-2012, 06:55 AM
dharav
Re: Programming exercise help
LOL Just reminded me of my olden days
• 01-13-2012, 07:16 AM
atac57
Re: Programming exercise help
Thanks, but I'm still having a problem. I want a negative integer entered once to kill the loop, but when I enter a negative it goes through the do loop and I have to enter a negative for every one of the variables to stop it. How can I fix that? Yes I know I'm a noob.

Code:

```import java.util.Scanner;   class Switch{     public static void main (String args[]){         Scanner scan = new Scanner(System.in);                 int first , last, gallons;         double milesGallon;                 System.out.println("Miles Per Gallon Program");                 do         {                                  System.out.println("Initial miles: ");             first = scan.nextInt();                            System.out.println("Final miles: ");             last = scan.nextInt();                         System.out.println("Gallons: ");             gallons = scan.nextInt();                         milesGallon = (last - first)/ gallons;               System.out.println("Miles per Gallon: " + milesGallon);             System.out.println();         }         while ( first >= 0);         {                 if (first < 0)                 {                 System.out.println("bye");                 }         }     } }```
• 01-13-2012, 04:37 PM