Guessing Game

/** * Guess at a computer generated random number. * * @Laura * @2.0 */ import java.util.*; import java.lang.Math.*; public class GuessingGame { public static void main(String[] args) { int win = 0; //initializing variables int guess1 = 0; int guess2 = 0; int guess3 = 0; int play = 1; //set to 1 to make sure the program always runs once while (play == 1) { win = (int)(Math.random() * 10 + 1); //generates random number between 1 and 10 Scanner s = new Scanner(System.in); System.out.println("You are playing a guessing game."); System.out.print("Enter a number between 1 and 10 for your first guess: "); //prompts for input guess1 = s.nextInt(); checkIfGuessIsInRange(guess1, s); //call to method at bottom if (guess1 == win) { //if first guess is correct, the game stops and declares you a winner System.out.println("Winner. The random number was " + win + "."); //tells what the random number was System.out.println("You guessed " + guess1 + "."); //tells what your guesses were System.out.print("Would you like to play again? Enter 1 to play again. Enter 2 to stop: "); //prompts for input again play = s.nextInt(); //re-assigns play to 1 or 2 to continue or exit the while loop } else { //the rest of the nested if-else statements are basically the same System.out.print("Wrong. Enter a number between 1 and 10 for your second guess: "); guess2 = s.nextInt(); checkIfGuessIsInRange(guess2, s); if (guess2 == win) { System.out.println("Winner. The random number was " + win + "."); System.out.println("You guessed " + guess1 + " and " + guess2 + "."); System.out.print("Would you like to play again? Enter 1 to play again. Enter 2 to stop: "); play = s.nextInt(); } else { System.out.print("Wrong. Enter a number between 1 and 10 for your third guess: "); guess3 = s.nextInt(); checkIfGuessIsInRange(guess3, s); if (guess3 == win) { System.out.println("Winner. The random number was " + win + "."); System.out.println("You guessed " + guess1 + ", " + guess2 + ", and " + guess3 + "."); System.out.print("Would you like to play again? Enter 1 to play again. Enter 2 to stop: "); play = s.nextInt(); } else { System.out.println("Sorry, you're a loser. The random number was " + win + "."); System.out.println("You guessed " + guess1 + ", " + guess2 + ", and " + guess3 + "."); System.out.print("Would you like to play again? Enter 1 to play again. Enter 2 to stop: "); play = s.nextInt(); } } } while (play != 1 && play != 2) { //catches if the user input is something other than 1 or 2 System.out.print("That is not a valid answer. Try again. Enter a 1 or a 2: "); play = s.nextInt(); } } System.out.println("Program terminated."); } public static void checkIfGuessIsInRange(int guess, Scanner s) { //method takes an int and a scanner if (guess < 1) { //if guess is outside of range, ask for value again System.out.print("That is not a valid answer. Try again. Enter a number between 1 and 10: "); guess = s.nextInt(); } else if (guess > 10) { System.out.print("That is not a valid answer. Try again. Enter a number between 1 and 10: "); guess = s.nextInt(); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.