I have a project to create a program that picks a number (1-10), then has the user guess. If they are too high it tells them so and same goes for if they guessed too low. Now to mistake-proof it, I have to add try and catch blocks. If you look at the code you will see that I have but when triggered the code processes the catch then finishes. How do I get it to loop back?
import java.util.*;
import java.lang.*;
class GuessDR {
public static void main(String[] args){
System.out.println("Welcome to my number guessing game!");
int maxnum = 10;
Scanner input = new Scanner(System.in);
Random rand = new Random();
int number = rand.nextInt(maxnum);
int tries = 0;
int guess;
boolean win = false;
try{
while (win == false){
System.out.println("Guess a number between 1 and "+
maxnum +": ");
guess = input.nextInt();
tries++;
if (guess == number){
win = true;
}
else if(guess < number){
System.out.println("Number is to low, tray again");
}
else if(guess > number){
System.out.println("Number is to high, try again");
}
}
}
catch (InputMismatchException e) {
System.out.println("Enter numerical guess");
}
System.out.println("You win!");
System.out.println("It took you "+ tries + " tries.");
}
}