File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Noob java assigment " Watch "Noob java assigment " New topic

Noob java assigment

Khoa Le

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

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15096

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
It is sorta covered in the JavaRanch Style Guide.
subject: Noob java assigment
It's not a secret anymore!