user5547835
user5547835

Reputation:

Java Guessing Game - Optimization and Hints

I'm having a small problem with my program. It's a simple Java guessing game where a random number is generated, and the user guesses the number. Upon every unsuccessful guess, the user is shown a "Too High" or "Too Low" message until they eventually find the number. After the number is found, the user is prompted whether to continue or quit the game.

Everything works except I just need to stop the program from printing a data entry box after the no answer is selected. I also would like some advice on optimization and taking the continue game input (Y or y) as yes instead of relying on numbers. I've looked at converting the string into an int and using the method I have now, but I can't help but feel there is an easier way.

I do apologize for the messiness and all, (I'm a first year CS student with mediocre understanding) and I'd really appreciate any suggestions or hints you can offer.

import java.util.Scanner;

public class NumberGame 
{
public static void main(String[] args) 
{
   Scanner keysIn = new Scanner(System.in);

   for (int x =1; x>0; x++)
//Infinite loop to be exited when user quits
   {
   System.out.println("I'm thinking of a number between 1 and 100.");
   System.out.println("What is it?");
   int num = (int) (Math.random()*100+1);
//Generate Random number between 1 and 100      
   while(true)
   {
     int num2 = keysIn.nextInt();
// initialize the variable num2 and set it to next integer input
     System.out.println("Guess:" + num2);

     if (num == num2) 
//If the generated number is equal to the guess
     {
       System.out.println("You go it!");
       System.out.println("Play Again? (Y = 1/N = 0)");
       Scanner scan = new Scanner(System.in);
       int desc = scan.nextInt();
//Make a new scanner and take the "descion" input whether to continue or quit (1 or 0)           
       if(desc != 0){
       break;
//If the input is not 0 break the loop and play perform actions again           
       }
       else{
         System.out.println("Thanks for playing");
         continue;
  //If the input is 0 break the loop and continue the program post-loop             

     }  
     }
     else if(num > num2){
     System.out.println("Too Low.");
//If the number generated is greater than the guessed number print "Too Low"        
     }
     else if (num <num2){
     System.out.println("Too High.");
  //If the number generated is less than the guessed number print "Too High"         
     }
   }
}
}
}

Upvotes: 0

Views: 821

Answers (2)

gonzo
gonzo

Reputation: 2121

Instead of breaking out of your loop, if you want to end the game with 0, I would just return so you do not get the data entry when exiting. Something like this:

else{
    System.out.println("Thanks for playing");
    return;
    //If the input is 0 return and exit the method             
}

To have the user enter letters instead of numbers try something like this (You can add or remove as many cases as you would like):

String desc = scan.nextLine();
if (desc.equals("N") || desc.equals("No")){
    System.out.println("Thanks for playing");
    return;
}
break;

Or you can use a switch just name your outer loop so when you call break, you break out of the outer loop and not just the switch.

Scanner keysIn = new Scanner(System.in);
for (int x =1; x>0; x++)
    //Infinite loop to be exited when user quits
{
    System.out.println("I'm thinking of a number between 1 and 100.");
    System.out.println("What is it?");
    int num = (int) (Math.random()*100+1);
    //Generate Random number between 1 and 100
    Outer: //Name loop for breaking later on
    while(true)
    {
        int num2 = keysIn.nextInt();
        // initialize the variable num2 and set it to next integer input
        System.out.println("Guess:" + num2);

        if (num == num2) 
            //If the generated number is equal to the guess
        {
            System.out.println("You go it!");
            System.out.println("Play Again? (Y = 1/N = 0)");
            Scanner scan = new Scanner(System.in);
            String desc = scan.nextLine();
            switch (desc){
            case "Y":
            case "Yes":
                break Outer;
            case "N":
            case "No":
                System.out.println("Thanks for playing");
                return;
            } 
        }
        else if(num > num2){
            System.out.println("Too Low.");
            //If the number generated is greater than the guessed number print "Too Low"        
        }
        else if (num <num2){
            System.out.println("Too High.");
            //If the number generated is less than the guessed number print "Too High"         
        }
    }
}

A switch statement is like if and else. If desc matches "Y" or "Yes" it will break out of the loop. If it matches "N" or "No" it will return and exit the method.

Hope this helps!

Upvotes: 1

Krishantha Dinesh
Krishantha Dinesh

Reputation: 377

import java.io.IOException;
import java.util.Scanner;

public class NumberGame 
{
public static void main(String[] args) throws IOException
{
   Scanner keysIn = new Scanner(System.in);

   for (int x =1; x>0; x++)
//Infinite loop to be exited when user quits
   {
   System.out.println("I'm thinking of a number between 1 and 100.");
   System.out.println("What is it?");
   int num = (int) (Math.random()*100+1);
//Generate Random number between 1 and 100      
   while(true)
   {

     int num2 = keysIn.nextInt();
// initialize the variable num2 and set it to next integer input
     System.out.println("Guess:" + num2);

     if (num == num2) 
//If the generated number is equal to the guess
     {
       System.out.println("You go it!");
       System.out.println("Play Again? (Y)");
       Scanner scan = new Scanner(System.in);
       char desc  = (char) System.in.read();;
//Make a new scanner and take the "descion" input whether to continue or quit (1 or 0)           
       if(desc == 'Y' || desc=='y'){
       break;
//If the input is not 0 break the loop and play perform actions again           
       }
       else{
         System.out.println("Thanks for playing");
         System.exit(0);
  //If the input is 0 break the loop and continue the program post-loop             

     }  
     }
     else if(num > num2){
     System.out.println("Too Low.");
//If the number generated is greater than the guessed number print "Too Low"        
     }
     else if (num <num2){
     System.out.println("Too High.");
  //If the number generated is less than the guessed number print "Too High"         
     }
   }
}
}
}

Upvotes: 0

Related Questions