0

方程式からa、b、およびcを取得し、それらを使用して次の式を使用してxを見つけるプログラムを作成しています: http ://www.purplemath.com/modules/quads/qform01.gif 。

取得する際の問題は、方程式1x ^ 2 + 3x +4をプラグインすると、x=1およびx=-4ではなく、x=-無限大およびx=無限大になることです。

これが私のコードです:

クラス1:

    public class quadratictest
    {
        public static void main(String args[])
        {
            DecimalFormat df = new DecimalFormat("#.###");
            System.out.println("--------------------------------------------------");
            System.out.println("               ~Quadratic Formula~");
            System.out.println("--------------------------------------------------");
            System.out.println("in a polynomial, there are 3 important numbers used");
            System.out.println("to figure out x. they are a, b, and c, shown below.\n");
            System.out.println("\t\t1x^2 +3x +4");
            System.out.println("\t\t^     ^   ^");
            System.out.println("\t\ta     b   c");
    Scanner input = new Scanner(System.in);
           System.out.print("\nPlease type a, b, and c here[a b c]: ");
            int a = input.nextInt();
            int b = input.nextInt();
            int c = input.nextInt();
            mathey quad = new quadsong(a,b,c);
        System.out.println("------------");
        System.out.println(quad.solveb());
        System.out.println(quad.solvea());
        //System.out.println("x =" +df.format(quad.solvea()));
        //System.out.println("x =" +df.format(quad.solveb()));
        System.out.println("------------");
    }
}

クラス2:

    import java.util.*;
    import java.io.*;
    import java.text.DecimalFormat;
            /**
     * Write a description of class quadsong here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class mathey
    {
        int a;int b;int c;
        double solution1;
        double solution2;
          public mathey(int aN, int bN, int cN)
        {
            int a  = aN;
            int b = bN;
            int c = cN;
            solvea();
            solveb();
        }
        public double solvea()
        {
         solution1 = ( (b*-1) + Math.sqrt((b^2)-(4*a*c)))/(a+a);
          if (solution1 == Math.floor(solution1))
         {
         return solution1;
        }
        else
        {
            return 0;
        }
        }
            public double solveb()
        {
         solution2 = ( (b*-1) - Math.sqrt((b^2)-(4*a*c)))/(2*a);
         if (solution2 == Math.floor(solution2))
         {
         return solution2;
        }
        else
        {
            return 0;
        }
        }
    }

ここに私の出力があります:

    --------------------------------------------------
                   ~Quadratic Formula~
    --------------------------------------------------
    in a polynomial, there are 3 important numbers used
    to figure out x. they are a, b, and c, shown below.

            1x^2 +3x +4
            ^     ^   ^
            a     b   c

    Please type a, b, and c here[a b c]: 1 3 4
    ------------
    x =Infinity
    x =-Infinity
    ------------

何が問題なのですか?前もって感謝します!PSコードのフォーマットについて申し訳ありませんが、idkなぜここで私が望むように機能しないのですか?

4

1 に答える 1

1

ここで問題になっているのは、1x ^ 2 + 3x+4にはルーツがないということです。それは何よりも数学の誤りです。

于 2013-01-31T18:46:57.133 に答える