Semester Review – Computer Science

When my Computer Science I students return in January, I will be giving them the following assignment:

Create a JAVA application that has users guess a pre-selected number as a game. The application should provide the rules of the game which state a random number between 0 and 100 will be chosen and users will be given a maximum of 20 guesses.

When asking the user for each guess, the application should indicate which guess of the 20 the user is on and whether their guess was too high or too low.

If the user guesses the solution in under 20 guesses, they will be congratulated and given the number of guesses it took for them to arrive at the solution.

If the user exceeds the maximum number of allowed guesses, they will be told they have exceeded the allowed number of guesses and will be provided with what the solution was.

While there are numerous ways this problem could be tackled, here is one possible solution that is similar to what I would expect at this point in the year:

//Program Name: Guess My Number
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2016
import java.util.*;
import java.lang.*;
import java.io.*;
public class guessNumber{
    public static void main (String[] args){
        System.out.println("Welcome to the Number Guessing Game");
        System.out.println("");
        System.out.println("I will pick a number at random and your job is to guess the number that I have chosen.");
        System.out.println("The number will be between 0 and 100. I will give you clues after each guess.");
        System.out.println("You will have up to 20 guesses to guess my number.");
        System.out.println("");
        Scanner ready = new Scanner(System.in);
        System.out.print("Are you ready to start? (Y or N)");
        String readyGo = ready.next();
        String readyGoUC = readyGo.toUpperCase();
        if (readyGoUC.equals("Y")){
            double randomNumber = Math.random() * 100;
            int randomNumberInt = (int)randomNumber;
            Scanner myGuessInput = new Scanner(System.in);
            System.out.print("What is your first guess? ");
            int myGuess = myGuessInput.nextInt();
            while(readyGoUC.equals("Y")){
                for (int score = 1; score < 20; score++){
                    if(randomNumberInt > myGuess){
                        System.out.println("Too Low. Try a Higher Number");
                        myGuessInput = new Scanner(System.in);
                        System.out.print("What is your #" + score + " guess? ");
                        myGuess = myGuessInput.nextInt();     
                    }else if(randomNumberInt < myGuess){
                        System.out.println("Too High! Try a Lower Number");
                        myGuessInput = new Scanner(System.in);
                        System.out.print("What is your #" + score + " guess? ");
                        myGuess = myGuessInput.nextInt();
                    }else if(randomNumberInt == myGuess){
                        System.out.println("");
                        System.out.println("Congratulations! You Guessed my Number!");
                        System.out.println("");
                        System.out.println("Number of Guesses = " + score);
                        System.out.println("");
                        System.out.println("Exiting Program");
                        System.exit(0);
                    }
                }
                System.out.println("");
                System.out.println("You have exceeded the maximum number of guesses.");
                System.out.println("");
                System.out.println("The number I picked was " + randomNumberInt);
                System.exit(0);
            }
        } else{
            System.out.println("Thank you for playing.");
            System.out.println("Exiting Program");
            System.exit(0);
        }
    }
}

This solution utilizes several of the skills covered in the first semester of Computer Science I including the use of character, string, integer, double, and scanner variable types. In addition, the toUpperCase and equals methods of the String class are used as is the random method of the Math class.

Also, in this possible solution, if/else, for, and while statements are all utilized along with various Boolean operators.

Leave a Reply

Your email address will not be published. Required fields are marked *