I FIGURED EVERYTHING OUT TY FOR THE HELP
When I type in letters at attempt 1/4 it works fine and it continues, but once I type a letter at attempt 2/4 it just prints the message and the program stops. Also any tips on #2, I can only think of if(guess>=4 && guess<=16) else statement(not sure if this is correct)
When I execute the code -
Guess a number between 1 and 16.
Attempt 1 of 4: 8
You guessed 8
Too Low!
Attempt 2 of 4: a
Please enter an integer between 4-16
can't enter anything after
Problems: I have to make exception handlers if user types in a
1) non-numeric input
2) out of range input
3) Have to retain current guess amount
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
static final int limit = 4;
static final int maxInteger = 16;
public static void main(String[] args) {
Random rand = new Random();
int target = rand.nextInt(maxInteger) + 1;
int x = 1;
do{
try{
Scanner input = new Scanner(System.in);
System.out.printf("Guess a number between 1 and %d.\n", maxInteger);
int attempts = 1;
while (attempts <= limit) {
System.out.printf("Attempt %d of %d: ", attempts, limit);
int guess = input.nextInt();
System.out.printf("You guessed %d\n", guess);
if(guess > target) {
System.out.printf("Too High! \n");
}
else if(guess == target){
System.out.printf("The answer is %d, You win!", guess);
attempts = 20;
}
else{
System.out.printf("Too Low! \n");
}
attempts+=1;
x = 2;
}
if(attempts==5){
System.out.println("You lose!");
}
}
catch(InputMismatchException e){
System.out.printf("Please enter an integer between 4-16");
continue;
}
}while(x == 1);
}
}