如何将 "double" 值与 JOptionPane.showInputDialog 一起使用

how do I use a "double" value with JOptionPane.showInputDialog

这是我的代码:

       currentSalary = (double)(JOptionPane.showInputDialog(frame, "Enter your current salary", "Current Salary", JOptionPane.PLAIN_MESSAGE, null, null, "Numbers only!"));
       raise = currentSalary * .04;
       total = currentSalary + raise;
       NumberFormat money = NumberFormat.getCurrencyInstance();
       System.out.println("Your raise is " + money.format(total));

目前,我需要将 currentSalary 设置为双精度值,但我做不到,因为 JOptionPane 只包含字符串。我将如何在仍然使用对话框的同时执行此操作?任何有效的答案表示赞赏。谢谢。

几天前我遇到了类似的问题。

我使用 Double.parseDouble() 将变量解析为 String ,来自 double

我认为代码应该如下所示...

            double  currentSalary = Double.parseDouble(JOptionPane.showInputDialog(frame, "Enter your current salaryy\", \"Current Salary\", JOptionPane.PLAIN_MESSAGE, null, null, \"Numbers only!\""));

如果我错了,请随时纠正我,我只是一个初学者,但这对我有用!

你也可以使用JOptionPane.showConfirmDialog,这将接受原始变量。