# Thread: Programming exercise help

1. Member
Join Date
Jan 2012
Posts
24
Rep Power
0

## Programming exercise help

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.

Java 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);
}
}
}```

2. ## Re: Programming exercise help

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.

3. Member
Join Date
Dec 2011
Posts
28
Rep Power
0

## Re: Programming exercise help

LOL Just reminded me of my olden days

4. Member
Join Date
Jan 2012
Posts
24
Rep Power
0

## 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.

Java 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");
}
}
}
}```

5. ## Re: Programming exercise help

If you want it to terminate on the first entry of a negative number, you need to check each each number as it's entered and then if negative, you can add a break statement. Break will 'break' it out of the loop.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•