Java -How do I check if a number is valid without looping or any iterative processes?
So I have a program that prompts the user to enter a number 1-5. (can include 1 and 5). It is to keep prompting the user until they enter a valid number. It's easy to do this with a while loop but for this assignment we are not supposed to use any type of loops or iterative processes. I am not good at recursion but I think that is the route ?
Please help, I would appreaciate actual code than pseudo . Thanks
import java.util.Scanner;
public class HelloWorld{
//Digites function is used to prompt user for input
public static int Digits()
{
Scanner input = new Scanner( System.in );
System.out.print("Please enter number: ");
int max = input.nextInt(); //variable max takes users input
//check if max is between 1-5
if (max==1||max==2||max==3||max==4||max==5)
return 0 ;
else
//if max is >5 then function Digit() is called again.
return Digits();
}
public static void main(String[] args)
{
int res=Digits() ;
}
}
-----------------------------------output-----------------------------------------
Get Answers For Free
Most questions answered within 1 hours.