Question

Java -How do I check if a number is valid without looping or any iterative processes?...

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

Homework Answers

Answer #1

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-----------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I've just finished typing my code in C, but I don't know how to draw a...
I've just finished typing my code in C, but I don't know how to draw a flowchart. Can somebody help me create a flow chart of this C program please? #include <stdio.h> int main(void){ char answer; // user's inputted single character int score = 0; // initialize score to 0 printf("-----------------------------------\n"); printf("\tMood Self Assessment\n"); printf("-----------------------------------\n"); printf("This Mood Self-Assessment program can help you determine and understand how\nyou are feeling recently."); printf("The user has to answer 3 questions honestly and\ntruthfully in order...