• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Boolean always returns false

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have been trying to write a code where 3 numbers are selected randomly and the user needs to try and guess them. but im having a problem with the Boolean returning true. so even if you guess all of the numbers correctly it says you have guessed none of the numbers. i have spent a ridiculous amount of time trying to fix this problem. im probably overlooking something, but i just cant spot whats going wrong. much appreciated on some feedback.

 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I recommend that you fix the compiler error first. Running code that doesn't compile is not a good idea.

https://coderanch.com/wiki/660183/Fix-Compiler-Errors-Running-Application

Henry
 
matthew reid
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe it was in the middle of my trying to fix it here is a copy i has no compile errors.

 
Henry Wong
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: You do know that the "first" variable in the getScoreOne() method, and the "first" variable in the main() method are different variables right?  And that ... changing one doesn't change the other?  And likewise for "second" and "third"?

Henry
 
matthew reid
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it to work now thanks here is a copy if you want to see

 
Master Rancher
Posts: 4223
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of these return statements:

and similar ones can just be:

as you don't use the local variable at all.  May as well get rid of it.
 
Dave Tolls
Master Rancher
Posts: 4223
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you have three methods that do exactly the same thing (getScoreOne/Two/Three).
You only need one and then just pass in the different values to check.
 
Henry Wong
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Additionally, the "first", "second", and "third" variables of the main() method are not used/needed either, so, they can also be removed.

Okay ... they were not used in the earlier code either, but with the latest version, since the OP reorganize the code to completely not need them, they definitely can be removed.

Henry
 
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

matthew reid wrote:. . .

Well done sorting out your problems, but I don't like doing arithmetic with Math#random. For one thing, you are calculating two “random” ints, converting them to a double, and then converting that back to an int. For another, that technique is error‑prone. A tiny change to a formula which returns a number between 0 and 9 can convert that to a formula which reliably returns a number between 0 and 0. You will find lots of discussion about random numbers here.
There is something not at all nice about your assignments in lines 17, 21, etc. Don't write
if (something) return true; else return false;
Don't try to combine assignment with returns, which is not good style. Simply write
return something;
That might come out as
return guess1 == number || guess2 == number || guess3 == number;
Your parameter names are not at all good to read; who knows what a ga gb and gc mean?
Don't use multiple ///// If you need a line end comment, start it with two slashes and make it short. Longer comments shou‍ld be in /* … */. Separate successive methods by an empty line (or maybe two empty lines) not multiple slashes.
Your lines of code are too long. I noticed that whilst writing this post and seeing horizontal scroll bars. The cause turns out to be the long comment on line 54 and lines 130‑133. You can shorten those lines by making them into several lines each; with a bit of clever indentation it will become much easier to read.
 
matthew reid
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for help everybody. i think i have cleaned up all the unnecessary junk. the only thing i couldn't really understand was fixing up the random. i had a look at that post but im still confused about it. but it is working the way i want with what i have so for now im happy but i'll try to understand how to do it differently in the future. here is my final version.

 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure, but you haven't taken note of most of our concerns. Your getScore method is badly named because it doesn't get a score, and it shou‍ld be reduced to a single statement. Your message method is dreadfully complicated; you don't need all those return statements; simply return message at the end. You can probably simplify it by adding the numbers of the guesses to the message, and then you only need three ifs.
Never use == true and == false, which are both poor style and error prone. Every now and again we see somebody who wrote = instead of == and now has three errors for the price of one.
If b represents any [Bb]oolean expression:-
Not if (b == true) ...
but if (b) ...
Not if (b == false) ...
but if (!b) ...
The same applies to loops. And != false is even worse to read Try to make your ifs and whiles use booleans evaluating to true rather than false because it makes the code easier to read.
 
Tell me how it all turns out. Here is a tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!