[英]while loop doesn't loop to the right place
所以这是我遇到的问题:while循环不会循环回到我想要的位置,应该在用户完成第一个输入后提示您输入车辆类型,但会转到数字英里。
String vehicleType = JOptionPane.showInputDialog(
"Enter the type of vehicle or Q to quit ");
while (!vehicleType.equalsIgnoreCase ("Q")) {
if (!(vehicleType.equalsIgnoreCase ("car")
|| vehicleType.equalsIgnoreCase ("truck")
|| vehicleType.equalsIgnoreCase ("suv")
|| vehicleType.equalsIgnoreCase ("motorcycle")))
{
validData = false;
vehicleType = JOptionPane.showInputDialog(
"Please enter a valid vehicle type from the assigned list ");
}
else {
validData = true;
do {
try {
milesTraveled = Double.parseDouble (JOptionPane.showInputDialog
(null, "Enter number of miles to be traveled ")); }
catch (NumberFormatException e)
//Keep requesting Vehicle details from user
while(user wants to keep going)
{
boolean userQuit = false
//Continue requesting Vehcile type until valid OR until user wants to quit
while(user has not entered Q or valid car type)
{
answer = prompt user to enter type with your Dialog
if(answer entered is "q")
{
userQuit = true
break;
}
else if(answer IS one of the vehicle types)
{
Assign answer to vehicle type
break;
}
}
if(userQuit)
break;
//The rest of your code. Make sure to set userQuit back to false
}
好的,所以这里提供的是您应将事件顺序更改为的基本伪代码。 在查看其是否有效之前,请参阅如何询问用户输入。 这意味着第二次,它将再次询问。
如果他们不输入有效数据,您将需要决定要做什么。 我可能会在Dialog提示和if语句周围编写一个while循环,以确定输入是否有效,如果无效,请再次询问。
在阅读Yitzih的评论后,实际上值得一提的是,在确认车辆类型有效之后,您需要将其应用于车辆类型。 我已经用另一个while循环编辑了代码,以使查看所需内容更加容易。
我也同意使用“ Q”作为退出条件并不是特别好,但是如果这是您留下的设计决策,那么上面的伪代码将满足这一需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.