Hoping someone can help me understand the following code from Head First Java. I managed to solve the puzzle through much trial and error however I still don't quite understand how it is coming to the result displayed in the solution. The console returns the text "Result: 543345" when I run the package but I'm getting completely lost following the maths. Really want to understand what is happening here before I move on in the book in case I've missed something, can someone enlighten me?
From my maths the "ivar" variable should have a value of 100000 by the time the first while loop exits but that doesn't seem to add up properly. Have pasted codes from both class files below if someone cares to take a look, did a bit of searching around online to see if I could find more info but they were all help solving the project rather than explaining the code itself. Cheers!
I haven’t got my page 91 (or any of the other pages) to hand at the moment, so I can’t confirm that you have copied the code correctly.
What they want you to do is go through the code with a pencil and paper. Find whoever said you would not need pencil and paper again, and shoot them. Then write down the initial values of x yIf you add columns for ivar*factor and ivar*(5-factor) you can add those values up and I hope get 543345. That is the idea behind those puzzles: to get you used to following that value of variables as execution proceeds.
Joined: Apr 07, 2013
Cheers for that, I actually tried doing it on paper earlier but must have got my wires crossed. This time I did it and it all clicked into place in my mind, totally understand it now!