• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Noob java assigment

 
Khoa Le
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment I am doing is almost complete but the while loop doesn't exit. The calculations are correct though.


 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic