我之前对相同的代码有一个疑问,但原因有所不同。我不想让这个问题变得过于混乱。这是参考链接:Having trouble with JOPtionPane
我正在检查用户是否未在showInputDialog中输入任何内容,或者显示替代消息并将其带回到原始提示,或者只是循环浏览同一对话框,直到他们正常关闭该对话框或通过输入继续一个数字。
其余代码通过退出程序而没有引发异常,完全按照我想要的方式执行。我希望所有inputDialogs的行为方式与我对测试部分的尝试方式相同,而不仅仅是第一个。
当前,它会在退出时抛出NPE异常,或者在没有输入的情况下按OK时出现空字符串错误。
这里是代码(investing1和investment2是静态方法:]
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);
}
}
只需添加对result.isEmpty()的调用
String result = JOptionPane.showInputDialog ("Input Text Here");
if( result.isEmpty() )
{
// quit the loop
}
删除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);}
这里是