Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
GuessingGame BlueJ Project
Click here to download a BlueJ project for GuessingGame.
Download file: GuessingGame.vls
GuessingGame Source Code
The Java source code for GuessingGame is below. Click on a file name to download it.
/* * Example GuessingGame: I'm thinking of a number 1 to 100 * guessing game. * * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012 * * $Id: GuessingGame.java 1957 2012-10-25 03:59:29Z terescoj $ */ import java.util.Random; import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { final int MAX_NUMBER = 100; Scanner keyboard = new Scanner(System.in); Random gen = new Random(); // intro message System.out.println("I'm thinking of a number 1 to " + MAX_NUMBER + ". Guess it!"); // pick our random answer int answer = gen.nextInt(MAX_NUMBER) + 1; // and a place to keep our guesses int guess; // keep track of the number of guesses int numberOfGuesses = 0; do { // read in the next guess System.out.print("Next guess? "); guess = keyboard.nextInt(); numberOfGuesses++; // see if it's higher or lower if (answer < guess) { System.out.println("Lower!"); } else if (answer > guess) { System.out.println("Higher!"); } } while (answer != guess); System.out.println("You got it! It took you " + numberOfGuesses + " guesses."); } }