GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Noob java assigment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Noob java assigment " Watch "Noob java assigment " New topic
Author

Noob java assigment

Khoa Le
Greenhorn

Joined: Feb 03, 2010
Posts: 1
The assignment I am doing is almost complete but the while loop doesn't exit. The calculations are correct though.


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to the Ranch!

In a while loop, the condition is checked at the top (beginning) of the loop. If that condition is true, then the entire body of the loop executes before the condition is checked again.

In other words, execution will not break out of the loop the moment a user enters -1.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

Welcome to the Ranch! Please UseCodeTags when you post source code, and make sure your code is properly indented. That way it is much easier to read. Being a moderator on the forum here, I have added the code tags to your post above.

What do you mean with "the while loop doesn't exit"; what happens when you enter -1 for the gallons?

I tried running your program, and when I enter -1 for the gallons, it first asks for the miles and then exits, as you would expect.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: Noob java assigment