1

我对相同的代码有一个先前的问题,但出于不同的原因。我不想让这个问题变得太混乱。这是供参考的链接:JOPtionPane 遇到问题

我正在检查用户是否没有在 showInputDialog 中输入任何内容,或者显示替代消息并将它们带回原始提示,或者只是循环通过相同的对话框,直到它们正常关闭它或通过输入数字继续.

通过退出程序而不抛出异常,其余代码完全按照我想要的方式执行。我希望所有 inputDialogs 的行为方式与我尝试对测试部分执行的操作相同,而不仅仅是第一个。

目前,它会在退出时抛出 NPE 异常,或者在没有输入的情况下点击 OK 时会抛出空字符串错误。

这是代码(投资1和投资2是静态方法):

public static void main(String[] args) 
{  
String initialAmt_Str, targetAmt_Str, interestPct_Str, years_Str, result;
double principle = 0, target = 0, interest = 0;
int again = JOptionPane.NO_OPTION, time = 0;

NumberFormat fmt = NumberFormat.getCurrencyInstance();

do {  
     Object[] options = {"Compute years to reach target amount",
        "Compute target amount given number of years"};

     int choice = JOptionPane.showOptionDialog(null, "Please choose what you would like to do.",
            "Investment Advisor", JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE, null, options, null);

     if (choice != JOptionPane.CANCEL_OPTION) 
     {

        if (choice == 1) 
        {        
           initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);

           if (initialAmt_Str != null) 
              principle = Double.parseDouble(initialAmt_Str);

下面是我试图检查空字符串的地方

           else {
              if (initialAmt_Str.isEmpty()){
              while (initialAmt_Str.isEmpty()) {
             initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);}}

              if (initialAmt_Str != null) 
                 principle = Double.parseDouble(initialAmt_Str);

              else
                 System.exit(0);}

结束测试部分

interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
                        + " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE); 

           if (interestPct_Str != null && !interestPct_Str.isEmpty()) 
              interest = Double.parseDouble(interestPct_Str);

           else 
              System.exit(0); 

           years_Str = JOptionPane.showInputDialog (null, "Enter the amount of years:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);

           if (years_Str != null && !years_Str.isEmpty()) 
              time = Integer.parseInt(years_Str);

           else 
              System.exit(0);  

           result = "Your target amount given the number of years is " + 
              fmt.format(investment2(principle, interest, time)) + ".";

           JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);

           again = JOptionPane.YES_OPTION;
        }
     }

     else 
        again = JOptionPane.NO_OPTION;


     if (choice == 0) 
     {
        initialAmt_Str = JOptionPane.showInputDialog (null,"Enter the principle:","Investment Advisor",
        JOptionPane.PLAIN_MESSAGE);

        if (initialAmt_Str != null && !initialAmt_Str.isEmpty()) 
           principle = Double.parseDouble(initialAmt_Str);

        else 
           System.exit(0);


        interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
                        + " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE); 

        if (interestPct_Str != null && !interestPct_Str.isEmpty()) 
           interest = Double.parseDouble(interestPct_Str);

        else 
           System.exit(0); 


        targetAmt_Str = JOptionPane.showInputDialog (null, "Enter your target amount:", "Investment Advisor", 
        JOptionPane.PLAIN_MESSAGE);


        if (targetAmt_Str != null && !targetAmt_Str.isEmpty())
           target = Double.parseDouble(targetAmt_Str);

        else
           System.exit(0);

        result = "You will reach your target amount in " + 
              investment1(principle, target, interest) + 
              (investment1(principle, target, interest) == 1 ? " year." : " years.");

        JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);

        again = JOptionPane.YES_OPTION;

     }

     if (again != JOptionPane.NO_OPTION) 
        again = JOptionPane.showConfirmDialog(null, "Find Another?", "", JOptionPane.YES_NO_OPTION, 
        JOptionPane.PLAIN_MESSAGE); 

} while (again == JOptionPane.YES_OPTION);
}
}
4

2 回答 2

4

只需添加对 result.isEmpty() 的调用

String result = JOptionPane.showInputDialog ("Input Text Here");

if( result.isEmpty() )
{
// quit the loop
}
于 2015-02-09T15:55:42.053 回答
0

删除 if 语句if (initialAmt_Str.isEmpty())并仅使用 while 循环。当用户输入空字符串或initialAmt_Str为空但当用户输入有效输入时,while 循环将始终为 true,while 循环变为 false,从而中断循环

else {
   while (initialAmt_Str.isEmpty()) {
             initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);}

              if (initialAmt_Str != null) 
                 principle = Double.parseDouble(initialAmt_Str);

              else
                 System.exit(0);}

这里是

于 2020-02-26T21:20:35.623 回答