需要阻止两个循环在循环内循环

时间:2015-10-08 19:39:24

标签: java jgrasp

所以我的程序会问两个问题,方向和里程。用户可以为方向和英里输入无限输入,但是一旦用户完成,他只需输入“完成”并且循环将中断并显示消息和结束。我无法让循​​环中断需要帮助。此外,当为numOfMiles键入一个字母时,程序才会结束。它假设给出错误消息并重新提示但我似乎无法使其正常工作。

import javax.swing.JOptionPane;
public class taxiService {

public static void main(String [] args) {

//Declare variables
double fareCharge = 5;
double totalMiles = 0;
double finalFareCharged = 0;
double finalxCoord = 0;
double finalyCoord = 0;
double numOfMiles = 0;
double finalCoord = 0;
String error = "Invalid data, please enter valid data!";

String directions = "";
boolean restart = true;
//Prompt for direction and miles
while(restart){ 

boolean reprompt = true;
boolean reprompt_SecondQuestion = true;

while (reprompt) {

   directions = JOptionPane.showInputDialog(null, "Please enter a direction: ");
   if (directions.equalsIgnoreCase("East")) {
      finalxCoord = finalxCoord + numOfMiles;
      reprompt = false;
      }
      else if (directions.equalsIgnoreCase("West")) {
               finalxCoord = finalxCoord - numOfMiles;
               reprompt = false;
               }
             else if (directions.equalsIgnoreCase("North")) {
                     finalyCoord = finalyCoord + numOfMiles;
                     reprompt = false;
                     }
                     else if (directions.equalsIgnoreCase("South")) {
                             finalyCoord = finalyCoord - numOfMiles;
                             reprompt = false;
                             }
             else {
                 reprompt = true;
                 JOptionPane.showMessageDialog(null, error);
             }    
        }
   while(reprompt_SecondQuestion)
   {
   numOfMiles = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter distance in miles: "));
   if (numOfMiles > 0) {
        totalMiles += numOfMiles;  
        reprompt_SecondQuestion = false;
        }
         else {
             JOptionPane.showMessageDialog(null, error);
            }
    }

    //Prompt user to type "done"
    //Capture user input
    if(directions.equalsIgnoreCase("Done"))

    {
       //Display direction and miles to user
       restart = false;
       break;
    }
    else
    {
    }

}

finalFareCharged = fareCharge + ((numOfMiles / .25) * 2);
JOptionPane.showMessageDialog(null, "miles: " + totalMiles + "\nDirection :" + directions + "\nFinal Charge: " + finalFareCharged + "\nCoordination: " + finalCoord);
}
}

我能够用它来阻止循环问题

   while(done) {
   end = JOptionPane.showInputDialog(null, "Is this your destination?(YES/NO)");
    if (end.equalsIgnoreCase("Yes")) {
       restart = false;
       reprompt = false;
       reprompt_SecondQuestion = false;
       done = false;
       break;
        }
        else if (end.equalsIgnoreCase("No")) {
                 restart = true;
                 done = false;
                  }
        else{
            JOptionPane.showMessageDialog(null, error);
            done = true;
            }
}

我仍然遇到numOfMiles的问题。如果输入了一封信,程序结束。它不会给出错误消息。我添加了reprompt_SecondQestion = true;但没有变化。

1 个答案:

答案 0 :(得分:0)

检查循环的状况。这样的东西就足够了:

if(directions.equalsIgnoreCase("Done"))

{
   //Display direction and miles to user
   reprompt_SecondQuestion = false;
   reprompt = false;
   restart = false;
   break;
}

此外,我无法从这个块所在的括号中看出,如果它在循环中的话。请检查,因为正确的格式将节省您的时间。