Upload your Guessing Game code is on your website and make sure that the indentation is correct and, if possible, your code is highlighted according to the syntax. (Try this method: Syntax highlighting in Google Sites.)
Also make sure that you have added comments to the code, to make it clear what each part of it is doing. You should add a comment to explain the whole routine, a comment to explain any loops, and comments to explain any key lines of code. If in doubt, add a comment. In my opinion you can have as many as one comment per line of code.
Now try to write the code using a very high-level pseudocode (ie like lines of English). You might start of something like:
Generate a random number
Get the first guess from the user
etc
Finally, answer these questions:
- What would happen if we hadn’t got another guess from the user at the end of the while loop?
- There is another type of while loop in Java, whose loop condition is tested at the end of the loop, not at the beginning. So instead of
while (condition) { stuff() }
, it’sdo { stuff() } while (condition)
. Rewrite your main loop using this type of while loop. You may find it convenient to use a boolean variable as a flag, or alternatively make use of thebreak
statement. - See if you can improve your guessing game in the following ways:
- When the user guesses correctly, tell them how many guesses it took.
- Allow the user to set the range of numbers from which the program’s random number will be generated.
- If you finish all of that, search for pseudocode on this blog and start reading the IB’s guidelines on pseudocode. See if you can re-write your game in approved pseudocode.