• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop not working as expected  RSS feed

 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner. I'm trying to write a program that accepts a score from its user, displays a corresponding grade and asks if the user wants to continue(loop). The loop aspect is not working.


 
Tehmina Yaseen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just put that code in main methods it's working fine now

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
input.nextBoolean() is expecting either the string "true" or the string "false".
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because you are using an assignment instead of a comparison in line 34:

What you are doing here is setting the variable res to false, instead of just checking if it is false. Comparison is done with == instead of =:

Better yet: Use the ! operator (the "not" operator) and do it like this:

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error with = false shows why you shouldn't use == false or == true.

By the way: that isn't a while loop; it is a do loop (or a do‑while loop). There is a subtle difference between the two, whih youwill doubtless find explained in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!