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.
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.
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.
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)
Daniele Barell wrote:Great score Charlie! Congratulations!
Liutauras Vilda wrote:Did you take test in Java or Swift? Just joking, congratulations, very well done
Ganesh Patekar wrote:Don't forget to add your name to OCAJP Wall of Fame
charlie swift wrote:Hi,
Yesterday on 22 January I took the OCAJP SE 8 test and passed with 94%. I'm really grateful to Jeanne Boyarsky and Scott Selikoff for writing such an amazing book for preparation of OCAJP. Hats off! Also, the Enthuware exam simulator is a really good one for practice tests but I found that it had comparatively more difficult questions than the real exam (but as they say you can never over prepare, right? )
Apart from that I did all the questions and the practice test from Java OCA programmer practice test book by Jeanne & Scott which really did prepare me to handle awkward looking syntax and recognise all those nuances which might result in a compile time error or a runtime exception.
Thank you all here at CodeRanch for clarifying and explaining the stuff that was confusing. You guys are really great!
CS