具有多个条件的Java While循环:数据验证问题

时间:2017-03-23 00:23:14

标签: java validation while-loop do-while multiple-conditions

我正在创建这个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:

1 个答案:

答案 0 :(得分:0)

我可以在while循环中发现一个问题。您想检查用户输入是否在范围内,但是您使用sc.nextDouble()读取输入2次。如果sc.nextDouble() > 3000为真,那么您将通过另一个sc.nextDouble()调用获得下一个值。这应该可以解释你的程序的行为。

您应该检查是否有sc.hasNextDouble()的nextDouble,然后将其保存到变量,例如double input = sc.nextDouble(),然后检查输入if (input > 0 && input <= 3000) break;的范围,这样您就可以摆脱循环。< / p>