Collie Mason wrote:Thank you!
That' a pleasure
I called the playGame() method in toString(), and it cleared up my issue. . . .
I'm afraid it hasn't sorted out your issue; all it means is that you are getting a non‑zero result when you print it out. You want to be able to do something like:-
Use
toString() to return a (maybe very short [one line]) description of the state of the game.
I appreciate the suggestion with random.nextInt(), and I may use that in future code. This project is me trying to get experience with Math.random().
I know the
Java™ Tutorials recommends that, but I think it is not‑quiite‑best code. If you go back to the link I gave you yesterday, you will see I said,
Putting the () in the wrong places around the cast will probably give you a pseudo‑random int between 0..0
And, as Paweł has explained, that is exactly what has happened. I am not quite sure about your arithmetic there; you appear to have
+ 1 twice. Work it out carefully assuming Math#random() returns 0.999999999 and your bounds are 0...20; I think there is a risk that your formula will return 1 until you get the () right, and then there is a risk of its evaluating to 21. The correct formula is
No it isn't. Use
Both those formulae give you a number in the range
1...upperBound inclusive. The commented‑out version gives you a range one too large; I shall let you work out which the “extra” potential result is.
Actually, if you are
testing your app, don't start with plain simple Random(). Use
Give the random object a “seed”; then you will always get the same sequence of numbers. When you have finished testing, delete the “seed” and you will get different sequences each time.
The
+ 1 part converts the numbers from the way computers usually count (
0...n − 1) to how people count
1...n