Guessing Game 2 (Debugging)

/** * Guess at a computer generated random number. * * @Laura * @2.1 */ import java.util.*; import java.lang.Math.*; import java.lang.Integer.*; public class GuessingGame2 { public static void main(String[] args) { int win = 0; //initializing variables int guess1 = 0; int guess2 = 0; int guess3 = 0; String play = "1"; //set to 1 to make sure the program always runs once while (play.equals("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); 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.nextLine(); //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.nextLine(); } 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.nextLine(); } 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.nextLine(); } } } while (play.compareTo("1") != 0 && play.compareTo("2") != 0) { //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.nextLine(); } while (!checkIfStringIsNumber(play)) { System.out.print("That is not a valid answer. Try again. Enter a 1 or a 2: "); play = s.nextLine(); } } System.out.println("Program terminated."); } public static boolean checkIfStringIsNumber(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } return true; } public static void checkIfGuessIsInRange(int guess, Scanner s) { if (guess < 1) { 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.