anjaneya2b anjaneya2b - 2 months ago
271 0

  1. Enter Parameters for the game
  2. Enter guesses for the computer generated number

Java

Number Guessing Game

//Anjaneya Bhardwaj 
	//Number Guessing Game
	package apcsjava;
	import java.util.InputMismatchException;
	import java.util.Random;
	import java.util.Scanner;
	public class AnjaneyaNumberGuesser {
		   private static Scanner input = new Scanner(System.in);
		public static void main(String[] args){ 
			Random guess = new Random();
	
		       int yes = 1; //For the play again part.
		       
		      try{
		       while (yes == 1) {
		    	   System.out.println("Lower Bound?");//User enters the lower parameter of the number.
		    	   int lower = input.nextInt();
		    	   System.out.println("Upper Bound?");//User enters the upper parameter of the number.
		    	   int upper = input.nextInt();
		    	   if(lower>upper){ //if the user enters a lower parameter that is bigger than the upper one.
		    	System.out.println("The lower number can't be more than the upper one.");
		    	System.out.println("Looks like you give up");
		    	break;
		    	   }
		    	   
		           int number = guess.nextInt(upper-lower)+lower;//To find the random number.
		   
		           
		           if (guessNumber(number, 7 ,lower, upper)) { //If the game ends ask user if they want to play again.
		               System.out.println("Do you want to play again? 1 = yes, 2 = no ");
		               yes = input.nextInt(); //reads the answer.
	
		               while (yes != 1 && yes != 2) { //If user enters some other number that is not 1 or 2..
		                   System.out.println("What are you doing? I said 1 = yes, 2 = no ");
		                   yes = input.nextInt();
		               }
		           } else {
		               yes = 2; //When answer is 2 do not play again.
		           }
		           }
		}
		      catch (InputMismatchException e) {//if user doesn't enter an integer, complain
		    		System.out.println("Not an int!  Guess you're giving up.");
		      }
		       input.close();
		   }
		    	  
		private static boolean guessNumber(int number, int tries, int lower, int upper) {
		       System.out.println("Guess a number " + lower + "-" + upper + ":");
		       int guess = input.nextInt();
		       
		       for (int i = 0; i < tries; i++) {
		           if (guess == number) { //gets correct number.
		               System.out.println("Correct! The number was " + number);
		               System.out.println("You guessed this in " + (i + 1) + " tries."); //To display number of tries.
		               return true;
		           } else if (guess > number && i < tries - 1) { //Not shown on last try as the loser text will be shown.
		               System.out.println("Your number is too high, guess again: ");
		               guess = input.nextInt();
		           } else if (guess < number && i < tries - 1) {
		               System.out.println("Your number is too low, guess again: ");
		               guess = input.nextInt();
		           }
		}
	
		       System.out.println("Sorry... you did not guess the number in " + tries);  //If it is not guessed in 7 tries.
		       System.out.println(" tries. " + "The correct number was " + number + ".");
		       return true;
		   }
		}
Comments