{"id":182,"date":"2011-05-17T13:14:31","date_gmt":"2011-05-17T03:14:31","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=182"},"modified":"2011-07-17T22:00:20","modified_gmt":"2011-07-17T12:00:20","slug":"high-guessing-game","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/high-guessing-game\/","title":{"rendered":"High Low Guessing Game"},"content":{"rendered":"

The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number.<\/p>\n

First thing we need is to generate a random number. To achieve this Java provides the java.util.Random class with its nextInt() method providing exactly what we need (see line 31).<\/p>\n

Then we want to repeatedly prompt the user to guess the number until they get it right. For this we can use a while loop (see line 36). For each loop iteration we prompt the user to make a guess, and read their input. That input is then compared with the random number we generated earlier.<\/p>\n

And that’s about, look over the code and ask if anything isn’t clear. Or better still become a member<\/a> and one of our Java mentors can guide you.<\/p>\n

\r\npackage com.learnjava.console;\r\n\r\nimport java.util.Random;\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * Write a guessing game where the user tries to \r\n * guess the number randomly picked by the computer.\r\n * For each guess tell the user if the \r\n * guess is too high, low or correct.\r\n * \r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\n\r\npublic class HighLowGuessingGame {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\t\/\/ Create a random number generator\r\n\t\t\r\n\t\tRandom random = new Random();\r\n\r\n\t\t\/\/ Use Scanner for getting input from user\r\n\t\t\r\n\t\tScanner scanner = new Scanner(System.in);\r\n\t\t\r\n\t\t\/\/ Use the random generator to \r\n\t\t\/\/ pick a number between 0 and 99 (inclusive)\r\n\t\t\r\n\t\tint number = random.nextInt(100);\r\n\t\tint guess = -1;\r\n\t\t\r\n\t\t\/\/ Loop until the user has guessed the number\r\n\t\t\r\n\t\twhile (guess!=number) {\r\n\t\t\t\r\n\t\t\t\/\/ Prompt the user for their next guess\r\n\t\t\t\r\n\t\t\tSystem.out.print("Enter your guess: ");\r\n\t\t\t\r\n\t\t\t\/\/ Read the users guess\r\n\t\t\t\r\n\t\t\tguess = scanner.nextInt();\r\n\t\t\t\r\n\t\t\t\/\/ Check if the guess is high, low or correct\r\n\t\t\t\r\n\t\t\tif (guess<number) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Guess is too low\r\n\t\t\t\t\r\n\t\t\t\tSystem.out.println("Too low, please try again");\r\n\t\t\t\t\r\n\t\t\t} else if (guess>number) {\r\n\r\n\t\t\t\t\/\/ Guess is too high\r\n\r\n\t\t\t\tSystem.out.println("Too high, please try again");\r\n\t\t\t\t\r\n\t\t\t} else {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Guess is correct !!\r\n\t\t\t\t\r\n\t\t\t\tSystem.out.println("Correct, the number was " + number);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need is to generate a random…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[10,45,44,6,46,11],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-2W","jetpack-related-posts":[{"id":375,"url":"https:\/\/learn-java-by-example.com\/java\/prompt-user-console-input-validate-response\/","url_meta":{"origin":182,"position":0},"title":"Prompt user for console input and validate their response","date":"December 6, 2015","format":false,"excerpt":"We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":182,"position":1},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":182,"position":2},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":43,"url":"https:\/\/learn-java-by-example.com\/java\/palindromes\/","url_meta":{"origin":182,"position":3},"title":"Palindromes","date":"April 13, 2011","format":false,"excerpt":"A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":238,"url":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","url_meta":{"origin":182,"position":4},"title":"Selection Sort","date":"June 19, 2015","format":false,"excerpt":"When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It's not a particularly efficient algorithm and really only suitable for\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":235,"url":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","url_meta":{"origin":182,"position":5},"title":"How to check if a Java String is an integer?","date":"June 5, 2015","format":false,"excerpt":"The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":10,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/182\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}