解决三次公式Java

时间:2018-11-03 02:24:11

标签: java math cubic

我正在尝试创建一个程序,在其中插入变量的输入时,它将求解根。这是我目前拥有的:

import javax.swing.JOptionPane;

public class perfTaskTest
{
    public static void main(String[]args)
    {
        double a = 0;
        double b = 0;
        double c = 0;
        double d = 0;
        double root1, root2, root3, rootsTester, place, place2, place3;
        String Input;
        Input = JOptionPane.showInputDialog(null, "What is your a value?");
        try{
            a = Integer.parseInt(Input);
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Not a number. Try again.");
            System.exit(0);
        }

        Input = JOptionPane.showInputDialog(null, "What is your b value?");
        try{
            b = Integer.parseInt(Input);
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Not a number. Try again.");
            System.exit(0);
        }
        Input = JOptionPane.showInputDialog(null, "What is your c value?");
        try{
            c = Integer.parseInt(Input);
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Not a number. Try again.");
            System.exit(0);
        }
        Input = JOptionPane.showInputDialog(null, "What is your d value?");
        try{
            d = Integer.parseInt(Input);
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Not a number. Try again.");
            System.exit(0);
        }
        place = Math.cbrt((((Math.pow(-b,3)/(27*Math.pow(a,3))) +
                (((b*c)/(6*Math.pow(a,2))) -((b)/(a*2))))));

        place2 = (Math.sqrt(Math.pow((((Math.pow(-b,3))/(27*Math.pow(a,3))) +
                (((b*c)/(6*Math.pow(a,2))) -((d)/(2*a)))), 2)+(Math.pow((((c)/(3*a))-
                        ((b*b)/(9*a*a))),3))));

        System.out.println(place);
        System.out.println(place2);
    }
}

它当前没有输出正确的place2,我不知道为什么它计算不正确(可能是括号)。

我正在使用的方程式:

image

0 个答案:

没有答案