double在执行if(answer.isEmpty())时不能取消引用



这是我的代码:

package logarithm;
import javax.swing.*;

public class Logarithm
{
    private static int i;

    public static void main(String[] args)
    {
        // TODO code application logic here
        do
        {
            double answer = new Double(JOptionPane.showInputDialog("What is the end result?"));
            if (answer.isEmpty())
            {
                JOptionPane.showInputDialog("What is the starting number?");
            }
            double start = new Double(JOptionPane.showInputDialog("What is the starting number?"));
            double step = new Double(JOptionPane.showInputDialog("How big are the steps?"));
            double mid = answer / start;
            double algerithm1 = Math.log10(mid);
            double algerithm2 = Math.log10(step);
            double var = algerithm1 / algerithm2;

            if (Double.isNaN(var))
            {
                JOptionPane.showMessageDialog(null, "The're is an error! You filled in a zero!", "Error", JOptionPane.ERROR_MESSAGE);
                i = 1;
            } else if (Double.isInfinite(var))
            {
                JOptionPane.showMessageDialog(null, "The number is infinite!", "Error", JOptionPane.ERROR_MESSAGE);
                i = 1;
            } else
                JOptionPane.showMessageDialog(null, "And the variable is " + var + ".", "variable", JOptionPane.INFORMATION_MESSAGE);

            if (i == 1)
            {
                JOptionPane.showMessageDialog(null, "Restarting!", "Restarting", JOptionPane.ERROR_MESSAGE);
            }
            int reply = JOptionPane.showConfirmDialog(null, "Try again?", "Restart", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
            {
                i = 1;
            } else
            {
                JOptionPane.showMessageDialog(null, "Thanks for using my program!", "Goobye", JOptionPane.INFORMATION_MESSAGE);
            }
        } while (i == 1);
    }
}

我在if (answer.isEmpty())收到错误请帮助我!

我是编码新手

尝试先检查String是否有效。你可以做

String s = JOptionPane.showInputDialog("What is the end result?");
if(s.isEmpty()){
    //Error String is Empty
}else{
    double d = Double.parseDouble(s);
}

最新更新