Beginner here, is there a way to pass a condition into a method? For example passing (n > 1 && n < 10)
and then the method can replace n
with some variable.
//request an int from the user
public static int requestInt(String request, /*SOME CONDITION*/)
{
Scanner input = new Scanner(System.in);
System.out.println(request);
int response;
while (true){
while (!input.hasNextInt()){
System.out.println("Invalid response");
input.next();
}
response = input.nextInt();
if(/*RESPONSE DOESN'T FIT THE CONDITION*/){
System.out.println("Invalid response");
}else{
return response;
}
}
}