Write a method with the following header:
public static int getValidInput(int low, int high, String message)
This method will return a user entered number between high and low inclusive. If a number is entered
that is not between high and low, string message will be printed to the screen. If high is less than low, a
-1 is returned. For example, given this call:
int input = getValidInput(0, 100, “Not a valid grade, Please re-
enter”);
The following could occur:
Please enter a grade: -5
Not a valid grade, please re-enter: 101
Not a valid grade, please re-enter: 9
Answer:
Give me a thumbs up if you like my work !!!
I have completed the given function and it is working perfectly.
Just save the code as Main.java and then compile & run.
It will run smoothly.
The below code has no-error and it is working perfectly.
Code:
import java.until.*;
import java.io.*;
public class Main
{
public static int getValidInput(int low, int high,
String message)
{
Scanner sc=new Scanner(System.in);
System.out.print("\nPlease enter a grade: ");
int grade=sc.nextInt();
if(high < low)
{
return -1;
}
int check=1;
while(check==1)
{
if(grade<low ||
grade>high)
{
System.out.print("\n"+message+": ");
grade=sc.nextInt();
}
else
{
check=0;
}
}
return grade;
}
public static void main(String[]
args)
{
int input=getValidInput(0,100," Not a valid grade, Please re-enter");
System.out.println("\nUser Input : "+input);
}
}
Get Answers For Free
Most questions answered within 1 hours.