This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: Noob java assigment
 
Similar Threads
Help with toString message
output prints null
Exception in Thread-Inventory Program
error message
Desperately need help writing Tester