Explore the English language on a new scale using AI-powered English language navigator.
Practice. Developing "number guessing game" step by stepIn the lesson we will practise using the basic Java tools learned in previous articles. To do it let's develop the "Guess game". Its rules are as follows:
Step 1. Class & main functionLet's call the class "NumberGuessingGame" and add empty main function. It's completely valid program you can compile and run, but it doesn't print anything to the console yet. public class NumberGuessingGame { public static void main(String[] args) {
} } Step 2. Secret numberTo propose a secret number, we declare a variable secretNumber of type int and use Math.random() function to give it random value in range 1..1000. public class NumberGuessingGame { public static void main(String[] args) { int secretNumber; secretNumber = (int) (Math.random() * 999 + 1); System.out.println("Secret number is " + secretNumber); // to be removed later } } Secret number is 230
Don't worry, if you don't understand how things with random work. It's not a subject of the lesson, so just believe it. Note, that program exposes secret number to player at the moment, but we will remove the line printing the proposal in the final version. Step 3. Asking user for a guessIn order to get input from user, we declare another variable guess of type int. Code, reading input from user is not to be discussed in detail here, so take it on trust. import java.util.Scanner;
public class NumberGuessingGame { public static void main(String[] args) { int secretNumber; secretNumber = (int) (Math.random() * 999 + 1); System.out.println("Secret number is " + secretNumber); // to be removed // later Scanner keyboard = new Scanner(System.in); int guess; System.out.print("Enter a guess: "); guess = keyboard.nextInt(); System.out.println("Your guess is " + guess); } }Secret number is 78 Step 4. Checking if guess is rightNow let's check if human player's guess is right. If so program should print corresponding message. Otherwise, tell user that a guess is smaller/greater than the proposed number. To test the program let's try all three cases (remember that we peeked the secret number). import java.util.Scanner;
public class NumberGuessingGame { public static void main(String[] args) { int secretNumber; secretNumber = (int) (Math.random() * 999 + 1); System.out.println("Secret number is " + secretNumber); // to be removed // later Scanner keyboard = new Scanner(System.in); int guess; System.out.print("Enter a guess: "); guess = keyboard.nextInt(); System.out.println("Your guess is " + guess); if (guess == secretNumber) System.out.println("Your guess is correct. Congratulations!"); else if (guess < secretNumber) System.out .println("Your guess is smaller than the secret number."); else if (guess > secretNumber) System.out .println("Your guess is greater than the secret number."); } } Secret number is 938 Secret number is 478 Secret number is 559 Things seem to be working ok. Step 5. Add triesAt the moment user has only one attempt to guess a number, which is, obvious, not sufficient. Our next step is about giving user as many attempts as one needs. For this purpose let's use do-while loop, because user must enter a guess at least once. import java.util.Scanner;
public class NumberGuessingGame { public static void main(String[] args) { int secretNumber; secretNumber = (int) (Math.random() * 999 + 1); System.out.println("Secret number is " + secretNumber); // to be removed // later Scanner keyboard = new Scanner(System.in); int guess; do { System.out.print("Enter a guess: "); guess = keyboard.nextInt(); System.out.println("Your guess is " + guess); if (guess == secretNumber) System.out.println("Your guess is correct. Congratulations!"); else if (guess < secretNumber) System.out .println("Your guess is smaller than the secret number."); else if (guess > secretNumber) System.out .println("Your guess is greater than the secret number."); } while (guess != secretNumber); } } Secret number is 504 Enter a guess: 777 Your guess is 777 Your guess is greater than the secret number. Enter a guess: 333 Your guess is 333 Your guess is smaller than the secret number. Enter a guess: 504 Your guess is 504 Your guess is correct. Congratulations! Final step. Make it glowJust before we consider the program to be complete, let's remove the code, used for debug purposes. import java.util.Scanner;
public class NumberGuessingGame { public static void main(String[] args) { int secretNumber; secretNumber = (int) (Math.random() * 999 + 1); Scanner keyboard = new Scanner(System.in); int guess; do { System.out.print("Enter a guess (1-1000): "); guess = keyboard.nextInt(); if (guess == secretNumber) System.out.println("Your guess is correct. Congratulations!"); else if (guess < secretNumber) System.out .println("Your guess is smaller than the secret number."); else if (guess > secretNumber) System.out .println("Your guess is greater than the secret number."); } while (guess != secretNumber); } } Enter a guess (1-1000): 500 Your guess is greater than the secret number. Enter a guess (1-1000): 250 Your guess is smaller than the secret number. Enter a guess (1-1000): 375 Your guess is smaller than the secret number. Enter a guess (1-1000): 437 Your guess is smaller than the secret number. Enter a guess (1-1000): 468 Your guess is smaller than the secret number. Enter a guess (1-1000): 484 Your guess is greater than the secret number. Enter a guess (1-1000): 476 Your guess is greater than the secret number. Enter a guess (1-1000): 472 Your guess is correct. Congratulations! Extra tasksIf you would like to practise on your own, there are suggestions of possible improvements:
Algorithmic part. Binary search algorithmHow would you answer the question: "How much attempts one needs the most to guess the number?" The right answer is 10. Check binary search algorithm explanation to see why. Partners Ads
|