Collie Mason wrote:Thank you! I called the playGame() method in toString(), and it cleared up my issue. However, now "computer" is only being assigned as 1, not somewhere in the range between 1 and the user's input (assigned as "upperBound"). How can I fix this?
I appreciate the suggestion with random.nextInt(), and I may use that in future code.
And as it turns out Math.random()
is the cause of your problem. Well, not the method itself, but it is very easy to put parentheses wrong when using it.
(int)Math.random() * ((upperBound) + 1) + 1
Math.random() returns a double greater than or equal to 0.0 and less than 1.0. When you convert such double to an int you always get 0.
0 * ((upperBound) + 1) + 1
Anything multiplied by 0 is 0.
0 + 1
Zero plus one equals one.