2011-03-08 20 views
0

首先,我必须说我对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

请告诉我们你的错误是什么。 – birryree 2011-03-08 05:23:32

+0

为什么有一个支架 - “return(number);” – Pushkar 2011-03-08 05:26:21

+0

如果这是作业,请在问题中提及。 – MAK 2011-03-08 05:38:18

回答

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; 
    } 
    } 
} 
+0

非常感谢您的帮助 – shavinda 2011-03-08 05:41:19

2

有几点:

  1. val功能目前不知道叫number变量。您想要将number值传递给val函数。
  2. 由于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); 
    } 
} 
+0

非常感谢您的帮助 – shavinda 2011-03-08 05:49:05

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: ")); 
    } 
} 
+0

非常感谢您的帮助 – shavinda 2011-03-08 05:47:59

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