2011-03-08 51 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; 
}