Guess - guess = scan.nextInt(); numGuesses = 1; tooHigh =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// **************************************************************** // Guess.java // // Play a game where the user guesses a number from 1 to 10 // // **************************************************************** import java.util.Scanner; import java.util.Random; public class Guess { public static void main(String[] args) { int numToGuess; //Number the user tries to guess int guess; //The user's guess int numGuesses; //Number of guesses int tooHigh; //Number of too high guesses int tooLow; //Number of too low guesses Random generator = new Random(); Scanner scan = new Scanner (System.in); //randomly generate the number to guess numToGuess = generator.nextInt(10) + 1; //print message asking user to enter a guess System.out.println("I'm thinking of a number!!"); System.out.print("Enter your guess: "); //read in guess
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: guess = scan.nextInt(); numGuesses = 1; tooHigh = 0; tooLow = 0; while (guess != numToGuess ) //keep going as long as the guess is wrong { numGuesses++; if (guess > numToGuess) { System.out.print("Too high, guess again: "); tooHigh++; } else { System.out.print("Too low, guess again: "); tooLow++; } guess = scan.nextInt(); } if (numGuesses == 1) System.out.println ("You got it, after only one guess!!"); else System.out.println("You got it, after only " + numGuesses + " guesses!!"); if (tooHigh == 1) System.out.println(tooHigh + " guess was too high."); else System.out.println(tooHigh + " guesses were too high."); if (tooLow == 1) System.out.println(tooLow + " guess was too low."); else System.out.println(tooLow + " guesses were too low."); } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Page1 / 2

Guess - guess = scan.nextInt(); numGuesses = 1; tooHigh =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online