首先,我不得不说我对Java很新。
我需要使用Double
输入Scanner
值,并需要检查它是否在给定范围内。如果它在给定范围内,则应返回该值,否则应要求重新输入新数字
我尽我所能,但有编译错误。请告诉我如何在我的代码中解决这个问题。
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);
}
}
}
答案 0 :(得分:2)
几点:
val
函数当前不知道名为number
的变量。您希望将number
值传递给val
函数。val
函数的返回类型为String
,因此必须返回String
。
private static String val(double number, double minValue, double maxValue){
if (number < minValue || number > maxValue ){
return "try again";
}
else{
return String.valueOf(number);
}
}
答案 1 :(得分:2)
除了有关val
函数的评论不知道number
是什么之外,以下是您实际希望程序执行的操作:
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;
}
}
}
答案 2 :(得分:0)
您可以摆脱大部分代码并执行以下操作:
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();
}
这是一个完整的程序,显示了代码片段:
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: "));
}
}
答案 3 :(得分:0)
这是我使用的课程的一部分。这很简单,如果你输入一个字符串就不会破坏。如果需要,您可以找到有关它的更多信息here。
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;
}