0

今夜終わらせる必要があるプログラムに取り組んでいますが、基本的には因数分解の安価なバージョンを実行します...問題は、数値が得られず、NaN. 私のコードは次のとおりです。

クラス1(本プログラムを扱う部分):

    System.out.println("--------------------------------------------------");
        System.out.println("                   ~Factoring~");
        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 +2x -3");
        System.out.println("\t\t^     ^   ^");
        System.out.println("\t\ta     b   c");
        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 factor = new mathey(a,b,c, chooser);
        System.out.print(factor.solvefact());

クラス 2:

    public class mathey
    {
        double a,b,c;

double solution1;
double solution2;
double discriminant;
double x1 = 0;
double x2 = 0;
  public mathey(int aN, int bN, int cN)
{
    a = aN;
    b = bN;
    c = cN;
    discriminant = (b*b)-4*a*c;
    solvea();
    solveb();
}
public String solvea()
{
    solution1 = (-1*b + Math.sqrt(discriminant))/(2*a);
    x1 = solution1;
    if(discriminant > 0) 
    { 
        return"x = " + solution1; 

    } 
    else if(discriminant == 0) 
    { 
        return "x =  " + solution1; 
    } 
    else 
    { 
        double root1complex = -b/(2*a); 
        double root1complex2 = Math.sqrt(-discriminant)/(2*a); 

        return root1complex + " + " + root1complex2 + " i "; 
    }
}
    public String solveb()
{
    solution2 = (-1*b - Math.sqrt(discriminant))/(2*a);
    x2 = solution2;
   if(discriminant > 0) 
    { 
        return"x = " + solution2; 

    } 
    else if(discriminant == 0) 
    { 
        return"x =  " + solution2; 
    } 
    else 
    { 
        double root1complex = -b/(2*a); 
        double root1complex2 = Math.sqrt(-discriminant)/(2*a); 

        return root1complex + " - " + root1complex2 + " i "; 
    }
}
public mathey(int aFact, int bFact ,int cFact, int chooser)
{
    a = aFact; b = bFact; c = cFact;
    discriminant = (b*b)-4*a*c;
    solvea();
    solveb();
    solvefact();
}
public String solvefact()
{
    String Answer = "";
    if((int)solution1 == solution1)
    {
         int wholeNum = (int)solution1/1;
         double numerator = (solution1%1) * 10;
         int denominator = 10;
         while(numerator > denominator) {
             denominator = denominator * 10;
            }   
         Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";

    }
    else
    {
        Answer +="( x + " +(solution1*-1) +")";



    }
    if((int)solution2 == solution2)
        {
            int wholeNum = (int)solution2/1;
            double numerator = (solution2%1) * 10;
            int denominator = 10;
            while(numerator > denominator) {
                denominator = denominator * 10;
            }   
            Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";


        }
        else
        {
          Answer +="( x + " +(solution2*-1) +")";  
        } 
    return Answer;
}

出力は次のとおりです。

    Choose a Way to Solve
    1. Quadratic Formula
    2. Factoring
    Which Method? [1/2]: 2
    --------------------------------------------------
                       ~Factoring~
    --------------------------------------------------
    in a polynomial, there are 3 important numbers used
    to figure out x. they are a, b, and c, shown below.

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

    Please type a, b, and c here[a b c]: 1 2 -3
    (10x + 10.0)(10x + -30.0)

これを修正するにはどうすればよいのでしょうか? (x + 3.0)(x-1.0)

4

2 に答える 2

1

4-param コンストラクターMathey()(呼び出しているコンストラクター) では、変数を再宣言a, b, cし、渡された値を割り当てて、0 (デフォルト) のままのインスタンス変数をマスクします。これらのローカル変数は、コンストラクター内でのみスコープ内にあります。solveA()とでsolveB()a, b, c再びインスタンス変数 (すべて 0) を参照するため、2*a= 0 で割ります。これによりsolution1、 とがsolution2等しくなりNaNます。

2 番目のコンストラクターの行を変更します (引き続き使用する場合)。

double a = aN, b = bN, c = cN;

a = aN, b = bN, c = cN;

マスキングの問題を解決します。doubleただし、インスタンス変数をs ではなくsにしたい可能性が高いintので、変更します

int a;int b;int c;

double a, b, c;

(このように同じ型の複数の宣言を行うことができます)。

コンストラクタが 2 つある理由がわからないMatheyので、2 番目のコンストラクタ ( ) を破棄しchooser?て最初のコンストラクタを使用するか、2 番目のコンストラクタも に値を割り当てるようにしますdeterminant

とにかく、これは始まりです。

于 2013-02-01T03:52:44.370 に答える
0

関数を単体テストして、コードの可能性があるかどうかを確認する必要があります。たとえば、倍数で {%} 演算子を使用していることに気付きました。その演算子は整数で機能し、NaN の結果を最後までドラッグする可能性があります。

また、 mathey() で変数を宣言してから、存在しない場所でそれらを solvea() solveb() で使用しようとしています。

于 2013-02-01T03:25:54.553 に答える