Guessing the number
import java.util.*;
public class LabExer5A
{
public static void main(String args[])
{
Scanner Input = new Scanner (System.in);
System.out.println("Guess a number between 1 and 50");
int JPS_Guess = Input.nextInt();
int JPS_CorrectNum = 25;
int JPS_NextGuess;
do
{
try
{
if (JPS_Guess == JPS_CorrectNum)
{
System.out.println("You got it in ");
}
else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
{
System.out.println("Too low. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
{
System.out.println("Too High. Try again.");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
else if(JPS_Guess < 1 || JPS_Guess > 50)
{
throw new InputMismatchException();
}
}
catch(InputMismatchException e)
{
System.out.println("Invalid value. Please enter a number between 1 and 50: ");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
catch (NumberFormatException ex)
{
System.out.println("Error - Enter Numerical Values Only");
JPS_NextGuess = Input.nextInt();
JPS_Guess = JPS_NextGuess;
}
}
while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number
}
}
}
}
I'm not sure where I made the mistake. Either the use of the throw catch or maybe because of the way I rerun the program. I'm pretty new to using exception and try-catch Please help and be kind. Thank you.