我是Java的新手,我被要求为学校课程编写一个二次方程求解器。我不确定我的代码是否遥不可及,但是我给出的任何输入都会获得输出" NAN"。
import javax.swing.JOptionPane;
import static java.lang.Math.sqrt;
public class FunTest
{
public static void main(String[] args)
{
String number1=JOptionPane.showInputDialog("Enter A");
int a=Integer.parseInt(number1);
String number2=JOptionPane.showInputDialog("Enter B.");
int b=Integer.parseInt(number2);
String number3=JOptionPane.showInputDialog("Enter C.");
int c=Integer.parseInt(number3);
double discriminantsquared=((b^2)-(4*a*c));
double discriminant=Math.sqrt(discriminantsquared);
double x1=(((b*-1)+discriminant)/(2*a));
double x2=(((b*-1)-discriminant)/(2*a));
String output=("x1= "+x1+"n"+"x2= "+x2);
JOptionPane.showMessageDialog(null, output);
}
}
b^2
不是" b to二",而是" b用2"。对于电源,您可以使用Math.pow(b, 2)
,或者简单地使用b * b
。