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.
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"?
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 should 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.
posted 2 years ago
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.
posted 2 years ago
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 should 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