我寫了一個描述多項式對象的類。我收集併發送係數數組到系統中,並且用戶希望爲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;
}
}
您需要一隻橡皮鴨 - 請參閱https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –
基於快速閱讀,您將覆蓋'this.evaluation' 。你可能意思是'雙重評估= Math.pow..' –
DUDE多數民衆贊成在那裏不好笑 – theredfox24