2017-03-23 5 views
0

私は、次のん、このJavaプログラムを作成しています:のJava:データ検証の問題

/*Prompt user to enter miles and miles per hour. 
Display approximate travel time in hours and minutes. 
Accept decimal entries. 
Prompt user to continue (if user enters “y” or “Y”). 
*Must* perform data validation: a. only numbers, b. miles range (> 0 and no more than 3000), c. MPH (> 0 and no more than 100). 
Hint: Use integer arithmetic and division and modulus operators to calculate hours and minutes. 

Example: miles: 100, MPH 65: 1 hr(s) 32 min(s)*/ 

私は正しい最終的な結果を持つことができるけど、私は問題を抱えていますデータ検証。エラー応答は、文字の入力、0より小さい数字、または何マイルかを尋ねると3000より大きい数字が入力されたときに発生するはずです(同じことがMPHを求められたらうまくいくはずですが、私が何マイルも働いていることを確認してデータを取得したとき)。

私は1つのwhileループにすべての検証を入れてしまいました。最初の2つの条件(文字と3000より大きい数字が入力されている場合)ではうまく動作しますが、3番目の条件と正しい入力(0から3000までの数字)が入力されると、プログラムは直ちにそれを受け入れず、入力は複数回入力する必要があります(画像を置くことができなかったため、端末出力の最後のコードブロックを参照してください)。

ありがとうございました!

import java.util.Scanner; 

public class timetravel 
{ 
    public static void main(String[] args) 
    { 

    double miles; 
    double MPH; 
    double calculate; 
    double hours; 
    double minutes; 
    char answer; 

    System.out.println("This program displays the approximate travel time in hours and minutes."); 
    System.out.println("It accepts decimal entries and MUST perform data validation: a. only numbers, b. miles range (>0 and no more than 3000), c. MPH (> 0 and no more than 100)."); 
    System.out.println(); 

    do { 
    Scanner sc = new Scanner(System.in);  
    System.out.print("Enter miles: "); 
    while((!sc.hasNextDouble()) || sc.nextDouble() > 3000 || sc.nextDouble() < 0){ 

     System.out.println("Not a valid input."); 
     System.out.print("Please enter only numbers greater than 0 and less than 3001: "); 

     sc.next();   
    } 
    miles = sc.nextDouble(); 


System.out.print("Enter MPH: "); 
    while(!sc.hasNextDouble()){ 
     System.out.println("Not a valid input."); 
     System.out.print("Please enter only numbers greater than 0 and less than 3001: ");   
     sc.next(); 
    } 

    MPH = sc.nextDouble(); 


    calculate = (miles/MPH); 
    hours = Math.floor(calculate); 
    minutes = (calculate * 60) % 60; 
    minutes = Math.floor(minutes);  


    System.out.println("Miles: " + miles + ", MPH " + MPH + ": " + hours + " hr(s) " + minutes + " min(s)"); 
    System.out.println("This is calculate: " + calculate); 

    System.out.println("Would you like to continue?: "); 
    answer = sc.next().charAt(0); 
    answer = Character.toUpperCase(answer);  
    } while(answer == 'Y');  

    } 

} 

ターミナル出力

Not a valid input. 
Please enter only numbers greater than 0 and less than 3001: 3002 
Not a valid input. 
Please enter only numbers greater than 0 and less than 3001: -1 
-1 
-1 
Not a valid input. 
Please enter only numbers greater than 0 and less than 3001: 30 
30 
30 
30 
Enter MPH: 

答えて

0

私はあなたのwhileループで一つの問題を発見することができます。ユーザー入力が範囲内にあるかどうかを確認したいが、sc.nextDouble()で入力を2回読み取っている。 sc.nextDouble() > 3000がtrueの場合は、sc.nextDouble()という別の呼び出しで次の値が得られます。それはあなたのプログラムの動作を説明するはずです。

そのようにあなただけのループから抜け出すdouble input = sc.nextDouble()のように、それを変数に保存し、入力if (input > 0 && input <= 3000) break;と範囲をチェックsc.hasNextDouble()とnextDoubleがあるかどうかを確認する必要があります。