无法计算表达式:计算错误的总和

时间:2017-01-31 01:01:46

标签: java expression

我写了一个描述多项式对象的类。我收集并将系数数组发送到类和用户希望为BUT计算多项式的值,这是出于某种神秘的原因,只有java知道该类不断发回一个完全错误的多项式计算。我已经在这工作了3个小时,并且无法找到解决这个危险问题的方法。这个课有什么问题?我已经将测试者课程加上了实际课程。 这是程序的输出顺便说一下: 2 2 2 2(这些是我输入的系数}您正在评估值为2.0  多项式的和是: 32.0

2x ^ 0 + 2x ^ 1 + 2x ^ 2 + 2x ^ 3(你可以看到这是错误的答案,应该是30)

import javax.swing.JOptionPane;


    public class copy1D{

        public static void main(String[]args)
        {

            String input;
            int degree;
            double number;


            input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
            degree = Integer.parseInt(input);
            degree= degree+1;
            int [] array = new int [degree];

            //creating array of coefficients
            for ( int i =0; i<=array.length-1; i++)
            {
                input = JOptionPane.showInputDialog(" Enter coefficients:");
                array[i] = Integer.parseInt(input);
            }

            //Printing out the coefficients to ensure they are correct
            for ( int i =0; i<=array.length-1; i++)
            {
                System.out.print(array[i] + " ");
            }



            Class1D c1d = new Class1D(degree, array);


            input = JOptionPane.showInputDialog(" Enter the number for which to evaluate the expression:");
                number = Integer.parseInt(input);
                System.out.println(" You are evaluating for a value of: " + number);
                System.out.println(" The sum of the polynomial is:");
            System.out.println(c1d.Evaluathepolynomial(number));

                for (int z=0; z <= array.length-1; z++)
                {
                    if (z<array.length-1)
                    System.out.print(array[z] + "x^" + z + "+");
                    if (z==array.length-1)
                    System.out.print(array[z] + "x^" + z);
                }



        }

    }


public class Class1D {

private int degree;
private int [] coefficient;
private double evaluation=0;

    public Class1D(int degree){
    this.degree =degree;
    }

    public Class1D(int degree, int[] a){

    this.degree =degree;
    this.coefficient = a.clone();
}

      public int []getCoefficient() {
          return coefficient;

      }
    public double Evaluathepolynomial(double value){
        for (int i =0; i<this.degree; i++)
        {
            this.evaluation= Math.pow(value,i) *this.coefficient[i];

            this.evaluation+= evaluation;

        }
        return evaluation;

}
}

2 个答案:

答案 0 :(得分:0)

这就是我解决这个问题的方法:每次循环执行后我开始打印出值,并且我意识到计算正在正确执行。但是不正确的部分是评估+ =评估;。它没有按照我想象的方式工作。无论如何......所以我创建了一个名为private double sum的新实例字段而不是写评估+ =评估我使用this.sum + =评估; 见下面的代码: 这也是新的OUTPUT。请注意,这次它显示的是正确的金额:

2 2 2 2(输入的系数) 您正在评估值:2.0(正在评估多项式的​​值)

2.0 4 8 16.0 16.0 (在我试图解决问题时,忽略上面只是调试值)  多项式的和是: 30.0(这是正确的总和) 2x ^ 0 + 2x ^ 1 + 2x ^ 2 + 2x ^ 3(这是被评估为x = 2的多项式)

import javax.swing.JOptionPane;


public class copy1D{

    public static void main(String[]args)
    {

        String input;
        int degree;
        double number;


        input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
        degree = Integer.parseInt(input);
        degree= degree+1;
        int [] array = new int [degree];

        //creating array of coefficients
        for ( int i =0; i<=array.length-1; i++)
        {
            input = JOptionPane.showInputDialog(" Enter coefficients:");
            array[i] = Integer.parseInt(input);
        }

        //Printing out the coefficients to ensure they are correct
        for ( int i =0; i<=array.length-1; i++)
        {
            System.out.print(array[i] + " ");
        }



        Class1D c1d = new Class1D(degree, array);


        input = JOptionPane.showInputDialog(" Enter the number for which to evaluate the expression:");
            number = Integer.parseInt(input);
            System.out.println(" You are evaluating for a value of: " + number);
            System.out.println(" The sum of the polynomial is:");
            c1d.Evaluathepolynomial(number);
            System.out.println(c1d.getEvaluation());

            for (int z=0; z <= array.length-1; z++)
            {
                if (z<array.length-1)
                System.out.print(array[z] + "x^" + z + "+");
                if (z==array.length-1)
                System.out.print(array[z] + "x^" + z);
            }



    }

}



    public Class1D(int degree){
    this.degree =degree;
    }

    public Class1D(int degree, int[] a){

    this.degree =degree;
    this.coefficient = a.clone();
}

      public int []getCoefficient() {
          return coefficient;

      }

    public void Evaluathepolynomial(double value){
        this.value =value;

        for (int i =0; i<this.degree; i++)
        {
            evaluation= Math.pow(this.value,i) *this.coefficient[i];
            System.out.println(evaluation);
            this.sum+= evaluation;


        }
        System.out.println(evaluation);
    }
        public double getEvaluation()
        {
            return sum;
        }



}

答案 1 :(得分:0)

首先,一个简单的例子 - 我的方法看起来像这样(使用问题中的其他所有内容):

public class Class1D {

    private int degree;
    private int [] coefficient;
    // private double evaluation=0; (Remove this!)

    public Class1D(int degree){
        this.degree =degree;
    }

    public Class1D(int degree, int[] a){

        this.degree =degree;
        this.coefficient = a.clone();
    }

    public int []getCoefficient() {
        return coefficient;  
    }

    public double Evaluathepolynomial(double value){

        // This will be the output:
        double total=0;

        // For each degree..
        for (int i =0; i<this.degree; i++)
        {

            // The current one is..
            double evaluation = Math.pow(value,i) *this.coefficient[i];

            // Add it into the total:
            // (same as total+=evaluation)
            total = total + evaluation;

        }

        // We're done! Return that total:
        return total;

    }
}

发生了什么事?

首先要记住的是,变量一次只能保留一件事。所以,如果你做这样的事情:

double a;
a=1;
a=2;
a=14;

// a is now 14.

A将是14。这些其他行基本上被忽略了,因为a只能保存你最后设置的那些。

好的,让我们来看看你原来的循环:

 for (int i =0; i<this.degree; i++)
 {
     this.evaluation= Math.pow(value,i) *this.coefficient[i];
     this.evaluation+= evaluation;

 }

 return evaluation;

首先,请记住,它所做的一切都是在循环中反复重复这些行 - 无论你指定多少次。因此,为了便于可视化,让我们说“度”是3并且解开循环 - 这只是意味着复制行并删除循环:

 // i=0
 this.evaluation= Math.pow(value,0) *this.coefficient[0];
 this.evaluation+= evaluation;

 // i=1
 this.evaluation= Math.pow(value,1) *this.coefficient[1];
 this.evaluation+= evaluation;

 // i=2
 this.evaluation= Math.pow(value,2) *this.coefficient[2];
 this.evaluation+= evaluation;

其次,值得一提的是this.evaluationevaluation指的是相同的变量

希望它开始变得更加清晰 - 循环的每个迭代都完全覆盖了前一个的结果。让我们进一步简化它:

// i=0
evaluation=2; // It's 2
evaluation+=evaluation; // 2+2; it's now 4

// i=1
evaluation=10; // It's 10. It's now like that 4 never even happened!
evaluation+=evaluation; // 10+10; it's now 20

这意味着在原始代码中,只有最后一个系数。如果4度,最后一个系数为2,则32出现如下:

 this.evaluation= Math.pow(2,3) *2; // 16
 this.evaluation+= evaluation; // evaluation = 16+16 = 32

简单的路线是引入第二个变量,因此解决方案更像是:

 a=0; // a is now 0

 b=14;
 a+=b; // a is now 14

 b=20;
 a+=b; // a is now 34

或者使用一个变量的其他替代方法是直接将其添加到:

 a+=14; // a is now 14

 a+=20; // a is now 34

看起来像这样:

total += Math.pow(value,i) *this.coefficient[i];

调试

希望上述内容有助于明确发生了什么,以及为什么上述方法和您的答案都有效。但重要的是,一次一行地执行代码并清楚地说明它应该做什么是成功调试的一个重要组成部分。许多人发现从字面上说出来真的很方便(我知道我这样做!)因为你会很快发现出错的方式。放弃println次来电或添加断点来仔细检查它实际上正在做你所描述的内容也很重要。