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 参数构造函数Mathey()(这是您正在调用的构造函数)中,您正在重新声明变量a, b, c并分配传递给它们的值,屏蔽保持等于 0(默认值)的实例变量。这些局部变量仅在构造函数的范围内。在solveA()andsolveB()中,a, b, c再次引用实例变量(全为 0),因此您将除以2*a= 0,这使得solution1andsolution2等于NaN

将第二个构造函数中的行(如果您继续使用它)从

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

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

解决掩蔽问题。不过,您很可能希望实例变量为doubles 而不是ints,因此请更改

int a;int b;int c;

double a, b, c;

(您可以像这样进行相同类型的多个声明)。

我不知道为什么你有两个Mathey构造函数,所以要么废弃第二个(什么是chooser?)并只使用第一个,或者确保第二个也为determinant.

无论如何,这应该是一个开始。

于 2013-02-01T03:52:44.370 回答
0

您应该通过对功能进行单元测试来检查您的代码是否有可能。例如,我注意到您使用了带有双数的 {%} 运算符。该运算符使用整数,并可能将 NaN 结果拖到最后。

您还在 mathey() 中声明变量,然后尝试在它们不存在的 solvea() solveb() 中使用它们。

于 2013-02-01T03:25:54.553 回答