[英]Java While-Loop with Multiple Conditions: Data Validation Issue
I'm creating this Java program which does the following: 我正在创建执行以下操作的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)*/
I am able to have the correct end result, but I am having an issue with data validation. 我可以得到正确的最终结果,但是数据验证存在问题。 An error response should occur when there is an input of a letter, a number less than 0, or a number greater than 3000 when it asks for how many miles (The same should work when asked for MPH, but I was going to edit that when I got the data validation for miles working).
当输入字母,小于0的数字或大于3000的数字时,如果它询问多少英里(应该要求MPH,同样的方法也可以,但是我要编辑它),则应该发生错误响应当我获得英里行驶的数据验证时)。
I ended up putting all that validation in one while-loop, and it works fine for the first two conditions (which are if a letter and a number greater than 3000 is entered), but whenever the third condition and the correct input (a number between 0 and 3000) are entered, the program won't immediately accept it, and the input would have to be entered multiple times (see last code block below of terminal output because I couldn't put a picture). 我最终将所有验证放到一个while循环中,并且对于前两个条件(如果输入了字母和数字大于3000)可以正常工作,但是只要第三个条件和正确的输入(一个数字)输入0到3000之间的数字),程序将不会立即接受该输入,并且必须多次输入该输入(请参见下面终端输出的最后一个代码块,因为我无法放置图片)。
Thank you for any guidance! 感谢您的指导!
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');
}
}
Terminal Output 终端输出
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:
I can spot one problem in your while
loop. 我可以在您的
while
循环中发现一个问题。 You want to check if the user input is in range, but you read input 2 times with sc.nextDouble()
. 您想检查用户输入是否在范围内,但是使用
sc.nextDouble()
读取了sc.nextDouble()
输入。 If sc.nextDouble() > 3000
is true then you get a next value with another call of sc.nextDouble()
. 如果
sc.nextDouble() > 3000
为true,则通过再次调用sc.nextDouble()
获得下一个值。 That should explain the behavior of your program. 那应该解释您的程序的行为。
You should check if there is nextDouble with sc.hasNextDouble()
then save it to a variable, like double input = sc.nextDouble()
and then check for range with input if (input > 0 && input <= 3000) break;
您应该使用
sc.hasNextDouble()
检查是否存在nextDouble,然后将其保存到变量中,例如double input = sc.nextDouble()
,然后使用输入检查是否存在范围if (input > 0 && input <= 3000) break;
so you just break out of loop. 所以你只是打破循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.