First of all, I have to say I'm quite new to Java.
I need to input a Double
value using Scanner
and need to check whether it is within the given range. If it's within the given range it should return the value, other开发者_开发问答wise it should ask to re-enter a new number
I tried my best, but there are compile errors. Please tell me how I should solve this in my code.
class Find {
public static void main(String args[]) {
System.out.println(val(1, 100));
Scanner input = new Scanner(System.in);
double number;
System.out.print("Enter a number: ");
number = input.nextDouble();
}
private static String val(int minValue, int maxValue) {
if (number < minValue || number > maxValue) {
return "try again";
} else {
return (number);
}
}
}
Along with the comments regarding your val
function not knowing what number
is, it appears that the following is what you actually want your program to do:
import java.util.Scanner;
class Find {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
double number;
do {
System.out.print("Enter a number: ");
number = input.nextDouble();
} while(!isValid(number));
}
private static boolean isValid(double number){
int minValue = 1;
int maxValue = 100;
if (number < minValue || number > maxValue ) {
System.out.println("Try again");
return false;
}
else {
return true;
}
}
}
A couple points:
- The
val
function currently does not know of a variable callednumber
. You want to pass thenumber
value to theval
function. - Since the
val
function has a returning type ofString
, it must return aString
.
.
private static String val(double number, double minValue, double maxValue){
if (number < minValue || number > maxValue ){
return "try again";
}
else{
return String.valueOf(number);
}
}
You can get rid of most of that code and do something like:
System.out.print( "Enter a number: " );
number = input.nextDouble();
while ((number < minValue) || (number > maxValue)) {
System.out.println( "Number out of range." );
System.out.print( "Enter a number: " );
number = input.nextDouble();
}
Here's a complete program showing that snippet in action:
import java.util.Scanner;
public class Find {
public static double getNum (
double minVal,
double maxVal,
String prompt,
String errPrompt
) {
Scanner input = new Scanner(System.in);
System.out.print (prompt);
double number = input.nextDouble();
while ((number < minVal) || (number > maxVal)) {
System.out.print (errPrompt);
number = input.nextDouble();
}
return number;
}
public static void main(String args[]) {
System.out.println (getNum (1, 100, "Enter a number: ", "Try again: "));
}
}
This is part a class I use. It's quite simple and it won't break if you enter a string. You can find more info about it here if you need it.
public int readInt(String prompt, int min, int max)
{
Scanner scan = new Scanner(System.in);
int number = 0;
//Run once and loop until the input is within the specified range.
do
{
//Print users message.
System.out.printf("\n%s > ", prompt);
//Prevent string input crashing the program.
while (!scan.hasNextInt())
{
System.out.printf("Input doesn't match specifications. Try again.");
System.out.printf("\n%s > ", prompt);
scan.next();
}
//Set the number.
number = scan.nextInt();
//If the number is outside range print an error message.
if (number < min || number > max)
System.out.printf("Input doesn't match specifications. Try again.");
} while (number < min || number > max);
return number;
} public int readInt(String prompt, int min, int max)
{
Scanner scan = new Scanner(System.in);
int number = 0;
//Run once and loop until the input is within the specified range.
do
{
//Print users message.
System.out.printf("\n%s > ", prompt);
//Prevent string input crashing the program.
while (!scan.hasNextInt())
{
System.out.printf("Input doesn't match specifications. Try again.");
System.out.printf("\n%s > ", prompt);
scan.next();
}
//Set the number.
number = scan.nextInt();
//If the number is outside range print an error message.
if (number < min || number > max)
System.out.printf("Input doesn't match specifications. Try again.");
} while (number < min || number > max);
return number;
}
精彩评论