簡體   English   中英

如何在 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 設為 double 值,但我不能,因為 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 ,這將接受原始變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM