2017-01-31 104 views
0

我寫了一個描述多項式對象的類。我收集併發送係數數組到系統中,並且用戶希望爲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

您需要一隻橡皮鴨 - 請參閱https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

+1

基於快速閱讀,您將覆蓋'this.evaluation' 。你可能意思是'雙重評估= Math.pow..' –

+0

DUDE多數民衆贊成在那裏不好笑 – theredfox24

回答

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調用或添加斷點來仔細檢查它實際上正在做什麼,你描述的也是至關重要的。

+1

非常感謝Luke花時間以這種方式解釋所有事情。所以你很好而且有才華?......以及你必須是什麼樣的人......無論如何,在花費數小時試圖找到問題後,我一定再也不會犯這個錯誤了!我會嘗試你的調試方法,儘管它當我在圖書館時不是最好的主意。祝你有個愉快的周 – theredfox24

+0

@ theredfox24哈哈非常感謝!我很謙虛:)沒有問題 - 我很高興它對你有意義;也有一個偉大的一週! –

0

這是我固定的問題:我開始打印出來每次循環執行後的值,我意識到計算正在正確執行。但是不正確的部分是評估+ =評估;它沒有按照我想象的方式工作會或應該。無論如何...所以我創建了一個名爲private double sum的新實例字段,而不是寫評估+ =評估我使用了this.sum + = evaluation; 請參閱下面的代碼: 這也是新的OUTPUT。請看到它顯示正確的總和此時:

2 2 2 2(已輸入的係數) 您正在評估值:(對於該多項式正被評估的值)2.0

2.0 4.0 8.0 16.0 16.0 (忽略上面剛剛調試值而我試圖解決的問題) 多項式的總和爲: 30.0(這是正確的總和) 2X^0 + 2×^ 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; 
     } 



}