如何退出Java中的do while循环?

时间:2015-11-18 19:31:10

标签: java swing loops do-while

将号码输出为电话号码格式的简单程序 我似乎无法退出循环,我不确定我的想法是什么 !PhoneNumber.equals(" 999&#34);当用户输入999时它将退出循环,但它不起作用。任何人都可以帮助我这里的代码

import javax.swing.*;

public class PhoneNumberFormat 
{

    public static void main(String[] args) 
    {
      String PhoneNumber;
      int numLength= 10;

      do
      {

          PhoneNumber = JOptionPane.showInputDialog(null, 
                 "Enter your 10 digit phone number or enter 999 to quit");
          while(PhoneNumber.length() != numLength)
          {
              PhoneNumber = JOptionPane.showInputDialog(null,
                  "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                      + " Enter a 10 digit Phone number");    
          }

            StringBuilder str = new StringBuilder (PhoneNumber);
            str.insert(0, '(');
            str.insert(4, ')');
            str.insert(5,' ');
            str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is " +str.toString());

      }while(!PhoneNumber.equals("999"));

    }
}

4 个答案:

答案 0 :(得分:5)

如果您希望在999存在时需要添加if条件来观察它。

public static void main(String[] args) {
    String PhoneNumber;
    int numLength = 10;

    do {
        PhoneNumber = JOptionPane.showInputDialog(null,
                "Enter your 10 digit phone number or enter 999 to quit");

        // add this condition to exit the loop, as well protect against NPE
        if (PhoneNumber == null || PhoneNumber.equals("999")) {
            break;
        }

        while (PhoneNumber.length() != numLength) {
            PhoneNumber = JOptionPane.showInputDialog(null,
                    "Error: You Entered " + PhoneNumber.length()
                            + " digits\nPlease"
                            + " Enter a 10 digit Phone number");

            //protect against NPE
            if(PhoneNumber == null) 
               PhoneNumber = "";
        }

        StringBuilder str = new StringBuilder(PhoneNumber);
        str.insert(0, '(');
        str.insert(4, ')');
        str.insert(5, ' ');
        str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is "
                + str.toString());

    } while (!PhoneNumber.equals("999"));

}

答案 1 :(得分:4)

你强迫这个数字是10位数,所以你期待什么?它永远不会等于三位数的“999”。

也许你打算这样做:

while(!PhoneNumber.startsWith("999"));

答案 2 :(得分:2)

如果您想使用999作为输入选项退出,请允许用户首先输入999作为有效输入。

  int numLength= 10;
  do
  {

      PhoneNumber = JOptionPane.showInputDialog(null, 
             "Enter your 10 digit phone number or enter 999 to quit");
      while(PhoneNumber.length() != numLength)
      {
          PhoneNumber = JOptionPane.showInputDialog(null,
              "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                  + " Enter a 10 digit Phone number");    
      }

在这里,如果输入的长度不是10,则拒绝考虑输入。

答案 3 :(得分:2)

具有固定潜力NPE和解决问题的代码应如下所示:

import javax.swing.*;

public class PhoneNumberFormat 
{

    public static void main(String[] args) 
    {
      String PhoneNumber;
      int numLength= 10;

      do
      {

          PhoneNumber = JOptionPane.showInputDialog(null, 
                 "Enter your 10 digit phone number or enter 999 to quit");
          while(PhoneNumber!=null && PhoneNumber.length() != numLength)
          {
              PhoneNumber = JOptionPane.showInputDialog(null,
                  "Error: You Entered " + PhoneNumber.length() + " digits\nPlease"
                      + " Enter a 10 digit Phone number");    
          }

            StringBuilder str = new StringBuilder (PhoneNumber);
            str.insert(0, '(');
            str.insert(4, ')');
            str.insert(5,' ');
            str.insert(9, '-');

        JOptionPane.showMessageDialog(null, "Your telephone number is " +str.toString());

      } while(!PhoneNumber.substring(0,3).equals("999"));

    }
}

修正在线

while(PhoneNumber!=null && PhoneNumber.length() != numLength)

while(!PhoneNumber.substring(0,3).equals("999"));