Knute Snortum wrote:One way you might think about getting rid of the inner loop is creating a variable something like "the number of times I have already seen a six."
There are many other minor improvements that you do to your program:
* Get the code out of main(), see MainIsAPain (that's a link).
* Only declare variables right where you need them.
* The Random object should be declared only once.
* Remove comments that just say what the code says (most of your comments)
* Use more descriptive variable names (ns -> numberOfSixes)
Piet Souris wrote:Another way, that saves you from all the bookkeeping and brings you into the realm of java 8 streams...
Suppose you have the integer sequence 1, 2, 3, .... Now, keep only those integers for which the roll of the die equals the number-to-be-rolled (call that number S), and from this reduced sequence take the first N numbers, where N is the number of times we must roll S. The last number of that reduced sequence is the required number of rolls. It should just take a few lines.
Carey Brown wrote:On line 26 you are testing numberOfSixes!=3, you've already done that test on line 23. If you clean this up on line 26 then line 28 should become a simple 'else'.
The use of your boolean 'NotThreeSixes' is redundant, you can just test numberOfSixes!=3.
Campbell Ritchie wrote:Because in the past we had frequent problems with people “correcting” mistakes in old posts, so the replies looked like nonsense. Shall I correct the code tags for you?
Don't use <> for code tags: it's , but the button is quicker. The HTML <code> tag changes the font and I have only seen it used in javadoc comments.