The function never returns -1, it just quits! This one does return -1 so that someone could use a while loop to get the digit:
public static int evalDigit(string in, int max) {
int result = -1;
try {
result = String.parseInt(in.trim());
if (result > max) {
System.out.println(String.format("The digit entered was too high (max is %d), try again", max));
result = -1;
}
if (result < 0) {
System.out.println("Negative digits not allowed, try again");
result = -1;
}
}
catch (NumberFormatException e) {
System.out.println("Invalid digit was entered, try again");
}
return result;
}
I just declared result as -1 because I didn't want the program to pass something if the data entered wasn't valid. Also, I wasn't allowed to use loops of any kind so yay more pain for me. It was an exercise to practice using recursive functions.
46
u/[deleted] Nov 13 '20 edited Nov 13 '20
The function never returns -1, it just quits! This one does return -1 so that someone could use a
whileloop to get the digit: