我是 Java 的初学者,但遇到问题时遇到了一些困难。我正在使用 JOPtionPane 向用户询问一些问题。但是,我的困难来自这样一个事实,即当用户单击 X 或取消程序中的任何位置时,我不知道如何正确处理异常。此外,当我查看 Java API 并了解如何实现标题时,我尝试了它,但现在应该将标题放在文本字段中。
从程序的其他部分退出也会在主线程中产生异常,所以我希望能够捕获这些异常并优雅地退出而不是出错。此外,在输入对话框中,标题“投资顾问”显示在文本字段内。我查看了 API,似乎我使用了正确的形式,但显然不是。与输入对话框的图标相同。我不希望它们显示,但是如果我将它们放在输入对话框中,程序不会编译并说找不到符号。它适用于选项对话框。
以上来自问题解决之前
非常感谢那些花时间阅读本文的人。
这是代码(注意invest1 和invest2 只是简单的静态方法和正确的公式):
编辑以下代码;只是尝试添加错误检查空字符串
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
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)
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)
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)
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)
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)
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);
}