Java 变量在一个实例中未使用,但在另一个实例中未使用

Java 变量在一个实例中未使用,但在另一个实例中未使用,java,string,swing,variables,Java,String,Swing,Variables,我正在使用JOptionPane而不是Scanner编写一个二次公式。其中一个变量(第12行和第17行,userInput)被标记为未声明,从而创建运行时错误。其他迭代似乎没有这个问题。我是一个初学者,但我已经尝试使用我的资源,几乎没有运气。谁能看出我错在哪里 /* */ package a3main2; import javax.swing.JOptionPane; public class A3main2 { public static void main(String[]

我正在使用JOptionPane而不是Scanner编写一个二次公式。其中一个变量(第12行和第17行,userInput)被标记为未声明,从而创建运行时错误。其他迭代似乎没有这个问题。我是一个初学者,但我已经尝试使用我的资源,几乎没有运气。谁能看出我错在哪里

/*

*/
package a3main2;

import javax.swing.JOptionPane;

public class A3main2 
{
    public static void main(String[] args) 
    {
        String userInput;

        JOptionPane.showMessageDialog(null, "Hooray for quadratic fun!");
        JOptionPane.showInputDialog(null,"Enter the value for 'a': ");
        Double a = Double.parseDouble(userInput);
        JOptionPane.showInputDialog(null,"Enter the value for 'b': ");
        Double b = Double.parseDouble(userInput);
        JOptionPane.showInputDialog(null,"Enter the value for 'c': ");
        Double c = Double.parseDouble(userInput);

        Double discriminant = Math.sqrt((Math.pow(b , b))- (4 * a * c));
        Double part1 = ((-(b)) / (2 * a));
        Double x = ((-(b)) + Math.sqrt(discriminant) / (2 * a));
        Double y = ((-(b)) - Math.sqrt(discriminant) / (2 * a));

        if (discriminant < 0)
        {
            JOptionPane.showMessageDialog(null, "The two roots are " + x + "i" + 
                    " and " + y + "i.");
        }          
        else if (discriminant > 0)
        {
            JOptionPane.showMessageDialog(null, "There are two roots: " + x + 
                    " and " + y + ".");
        }
        else if (discriminant == 0)
        {
            JOptionPane.showMessageDialog(null, "There is only one root: " + x + 
                    ".");
        }
        else if (a == 0)
        {
            JOptionPane.showMessageDialog(null, "There is only one root: " + part1 
            + ".");
        }
        System.exit(0);
        }

    }
}
/*
*/
包装A32;
导入javax.swing.JOptionPane;
公共级A32
{
公共静态void main(字符串[]args)
{
字符串用户输入;
showMessageDialog(null,“为二次型乐趣万岁!”);
showInputDialog(null,“输入'a'的值:”);
Double a=Double.parseDouble(用户输入);
showInputDialog(null,“输入'b'的值:”);
Double b=Double.parseDouble(用户输入);
showInputDialog(null,“输入'c'的值:”);
Double c=Double.parseDouble(用户输入);
双判别式=Math.sqrt((Math.pow(b,b))-(4*a*c));
双部分1=((((b))/(2*a));
双x=((((b))+数学sqrt(判别式)/(2*a));
双y=((((b))-数学sqrt(判别式)/(2*a));
if(判别式<0)
{
showMessageDialog(null,“两个根是”+x+“i”+
“和“+y+”i.”;
}          
else if(判别式>0)
{
showMessageDialog(null,“有两个根:”+x+
"及"y+";;
}
else if(判别式==0)
{
showMessageDialog(null,“只有一个根:”+x+
".");
}
如果(a==0),则为else
{
showMessageDialog(null,“只有一个根:”+part1
+ ".");
}
系统出口(0);
}
}
}
您需要执行以下操作

userInput = JOptionPane.showInputDialog(null,"Enter the value for 'a': ");
Double a = Double.parseDouble(userInput);  

在输入输入的所有不同情况下。

方法中声明的所有变量都需要初始化才能使用,因为它们没有默认值(如类成员)。

您的变量
userInput
保持为空

应将用户输入保存在userInput变量中,请尝试以下操作:

userInput = JOptionPane.showInputDialog(null,"Enter the value for 'a': ");

哇,太谢谢你了!我觉得自己是个新手,所以非常感谢您的帮助。还要注意
System.exit(0);}应该是
系统退出(0)。。