Class FiveDigitNumber

java.lang.Object
FiveDigitNumber

public class FiveDigitNumber extends Object

Write a number guessing game where the user is trying to guess a randomly generated 5 digit number. After the user enters a number, the computer prints a string made up of <, >, or = characters. For each digit the string indicates if the number is greater than the user's guess (>), less than the user's guess (<) or equal to the user's guess (=).

For example. the user types 12345 and the computer responds with ><>== This response tells the user that the computer's number starts with a digit > 1 followed by a digit < 2, a digit > 3, and the last two digits are correct.

Sample Execution:

Enter a five digit number: 
55555
><>=<
Enter a five digit number: 
72752
=<>=>
Enter a five digit number: 
71853
=<==>
Enter a five digit number: 
70854
=====
You win in 4 guesses.

With the right approach, it is always possible to guess the number in 4 or fewer steps.

  • Method Details

    • main

      public static void main(String[] args)

      The following psuedocode describes the execution of the program:

       1. Pick a random 5 digit number.
       2. Repeat the following until the user guesses the right value
       2.1 Read a number from the user
       2.2 Check if it is 5 digits, if so do the following
       
       2.2.1 For each of the five locations:
       2.2.1.1 if the user's digit is smaller, add '>' to the output String.
       2.2.1.2 if the user's digit is larger add '<' to the output String.
       2.2.1.3 if the digits are the same, add '=' to the output String.
       
       2.2.2 Print the output string
       2.2.3 Update the number of guesses
       
       2.3 if not (from 2.2), print out an error message
       
       3. (after step 2's loop) Print out how many guesses it took the user.
       
      Parameters:
      args - Command line arguments.