I'm trying to use a method to display an error message if the user enters a number other than 1 - 4, but I'm getting a missing return statement error.
public int CheckAnswers () {
boolean incorrectAnswer = true;
do {
playerAnswer = CheckAnswers();
if (playerAnswer < 1 || playerAnswer > 4) {
System.out.println("You have entered an incorrect number.");
System.out.println("Please enter a number between 1 and 4");
} else {
return (playerAnswer); }
} while (incorrectAnswer);
}
The error points to the last bracket. I've done some looking around online and I think the problem is that I don't have a return statement in both parts of the if-else statement. But if they have entered an incorrect number I don't want to return anything. I tried using the below code unsuccessfully.
public int CheckAnswers () {
boolean incorrectAnswer = true;
do {
playerAnswer = CheckAnswers();
if (playerAnswer < 1 || playerAnswer > 4) {
System.out.println("You have entered an incorrect number.");
System.out.println("Please enter a number between 1 and 4");
return (null);
} else {
return (playerAnswer); }
} while (incorrectAnswer);
}