簡體   English   中英

具有多個條件的Java While-Loop:數據驗證問題

[英]Java While-Loop with Multiple Conditions: Data Validation Issue

我正在創建執行以下操作的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,同樣的方法也可以,但是我要編輯它),則應該發生錯誤響應當我獲得英里行駛的數據驗證時)。

我最終將所有驗證放到一個while循環中,並且對於前兩個條件(如果輸入了字母和數字大於3000)可以正常工作,但是只要第三個條件和正確的輸入(一個數字)輸入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:

我可以在您的while循環中發現一個問題。 您想檢查用戶輸入是否在范圍內,但是使用sc.nextDouble()讀取了sc.nextDouble()輸入。 如果sc.nextDouble() > 3000為true,則通過再次調用sc.nextDouble()獲得下一個值。 那應該解釋您的程序的行為。

您應該使用sc.hasNextDouble()檢查是否存在nextDouble,然后將其保存到變量中,例如double input = sc.nextDouble() ,然后使用輸入檢查是否存在范圍if (input > 0 && input <= 3000) break; 所以你只是打破循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM