如果用户输入的数字不是 1 - 4,我正在尝试使用一种方法来显示错误消息,但是我收到了缺少返回语句的错误。
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);
}
错误指向最后一个括号。我在网上做了一些环顾四周,我认为问题在于我在if-else 语句的两个部分都没有返回语句。但如果他们输入了错误的号码,我不想返回任何东西。我尝试使用以下代码失败。
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);
}