Home > database >  Building a Computer Guesser Game in Java with an exception handler. All of my variables are Underlin
Building a Computer Guesser Game in Java with an exception handler. All of my variables are Underlin

Time:01-17

Hello Stackoverflow Community. I am brand new to programming in Java and I've been learning to build exception handlers for bad inputs. I've been facing an odd response to my program compiling. The program is relatively small and I put it below:

public static void main(String[] args) {
    while (ehandle){
        Random generator = args.length == 0 ? new Random() :
    
        new Random(Integer.parseInt(args[0]));
    
        int secret = generator.nextInt(500);
    
        int guesscount = 0;
    
        int fullguesscount = 0;
    
        boolean ehandle = true;
        
        Scanner in = new Scanner(System.in);
        
        System.out.println("I am thinking of a number from 0-500.");
        
        System.out.println("Try to guess my number: ");
        
        String n = in.next();
        try{
            n = Integer.parseInt(n);
            while(n!=secret){
            guesscount  ;
            fullguesscount  ;
            
            if(n>secret) {
                System.out.println("My number is lower than "   n   " Guess again: ");
            } 
            else {
                System.out.println("My number is higher than "   n   " Guess again: " );
            }
                    if (guesscount > 1 && n>secret){
                        if(n > secret){
                            System.out.println("Bad Guess. I told you number is lower!");
                            guesscount = 0;
                            continue;
                        }
                    }
                    if (guesscount > 1 && n<secret){
                        if(n < secret){
                            System.out.println("Bad Guess. I told you that my number is greater!");
                            guesscount = 0;
                            continue;
                        }
                    }
            }
                    
            System.out.println("Congratulations! It only took you "   fullguesscount   " guesses!");
                    
            }
            catch(Exception e){
                System.out.print("Bad input");
                continue;
            }
        }
    }
}

Any advice is greatly appreciated.

CodePudding user response:

The program has multiple errors:

  1. If you read the data with Scanner.next(), the user cannot re-enter the value in the loop; so this line inside the loop changed to Scanner.nextLine().
  2. You should read the user input after the loop is entered; so I changed the while loop to do-while.
  3. Two different variables named n are defined; I set the name of the variable of type integer as _n.
  4. The variable ehandle is not defined.
  5. In order to avoid optimization problems inside the loop, it is useful to define some objects within the scope of the main().
import java.util.Random;
import java.util.Scanner; 

public class HelloWorld
{
    
    public static void main(String[] args) 
    {
/*@4*/  boolean ehandle = true;
/*@5*/  Random generator = (args.length == 0) ? (new Random()) : (new Random(Integer.parseInt(args[0])));
/*@5*/  Scanner in = new Scanner(System.in);
        
/*@4*/  while(ehandle)
        {         
            int secret = generator.nextInt(500);
            int guesscount = 0;
            int fullguesscount = 0;  
            System.out.println("I am thinking of a number from 0-500.");
            System.out.println("Try to guess my number: ");
            System.out.println("Secret: "   secret);
            
            try
            {       
/*@3*/          String n;
/*@3*/          int _n;
                
/*@2*/          do  
                {           
/*@1*/              n = in.nextLine(); 
/*@3*/              _n = Integer.parseInt(n);
                    guesscount  ;
                    fullguesscount  ;
                    
                    if(_n > secret) 
                        System.out.println("My number is lower than "   n   " Guess again: ");
                    else 
                        System.out.println("My number is higher than "   n   " Guess again: " );
                    
                    if (guesscount > 1 && _n > secret)
                    {
                        if(_n > secret)
                        {
                            System.out.println("Bad Guess. I told you number is lower!");
                            guesscount = 0;
                            continue;
                        }
                    }
                    
                    if (guesscount > 1 && _n < secret)
                    {
                        if(_n < secret)
                        {
                            System.out.println("Bad Guess. I told you that my number is greater!");
                            guesscount = 0;
                            continue;
                        }
                    }
/*@2*/          } while(_n != secret);
                        
                System.out.println("Congratulations! It only took you "   fullguesscount   " guesses!");
                break;
            }
            catch(Exception e)
            {
                System.out.print("Bad input");
                continue;
            }
        }
    }
}
  •  Tags:  
  • Related