【发布时间】:2015-05-22 17:29:40
【问题描述】:
我正在使用 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);
}
}
}
【问题讨论】:
-
还要注意
System.exit(0); }应该是System.exit(0);..
标签: java string swing variables