如何在方法中获取变量值?

时间:2018-01-20 22:58:00

标签: java arrays variables methods

是否有可能获得在公共多项式(double [] c)中建立的c的值抛出IllegalArgumentException?当我使用naiveEval方法时,我打印出值来检查它们是否到达。变量p和x都到达,但double [] c返回null。这有什么原因吗?我怎样才能解决这个问题?提前致谢!

public class PolynomialDemo  {
public static int deg;
public static int deg2;
public static double x;
public static double x2;
public static double[] numbers;
public static double[] numbers2;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Polynomial pol = new Polynomial();
    Polynomial pol2 = new Polynomial();

    Scanner scan1 = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);
    Scanner scan3 = new Scanner(System.in);
    Scanner scan4 = new Scanner(System.in);


    System.out.print("Enter the degree of the first polynomial -> ");
    deg = scan1.nextInt();

    System.out.print("Enter its coefficients in order of descending powers -> ");
    String coe = scan2.nextLine();
    String[] numbersStr = coe.split(" ");
    double[] numbers = new double[ numbersStr.length ];
    for ( int i = 0; i < numbersStr.length; i++ )
    {
        numbers[i] = Integer.parseInt( numbersStr[i] );


    }

    pol.setDegree(deg);
    pol.setCoefficients(numbers);


 System.out.println("f(x) = " + pol.toString());
 System.out.println("deg f(x) = " + deg);

 System.out.print("\nEnter the value of x at which the polynomial is to be evaluated -> ");
 x = scan3.nextInt();


 System.out.println("Using Horner’s method, f(" + x + ") = ");
 System.out.println("Using naive method, f(" + x + ") = " + pol.naiveEval(x));

 System.out.print("\nEnter the degree of the second polynomial -> ");
 deg2 = scan1.nextInt();

 System.out.print("Enter its coefficients in order of descending powers -> ");
 String coe2 = scan2.nextLine();
    String[] numbersStr2 = coe2.split(" ");
    double[] numbers2 = new double[ numbersStr2.length ];
    for ( int i = 0; i < numbersStr2.length; i++ )
    {
        numbers2[i] = Double.parseDouble( numbersStr2[i] );


    }

    pol2.setDegree(deg2);
    pol2.setCoefficients(numbers2);

    System.out.println("f(x) = " + pol2.toString());
    System.out.println("deg f(x) = " + deg2);

 System.out.print("\nEnter the value of x at which the polynomial is to be evaluated -> ");
 x2 = scan4.nextInt();

 System.out.println("Using Horner’s method, f(" + x2 + ") = ");
 System.out.println("Using naive method, f(" + x2 + ") = ");
}      
}

然后是我的多项式课程

public class Polynomial implements PolyEval  {

private double[] coeffs;
private int degree;
private double p;
private double[] c;

public void setDegree(int degree){
   this.degree = degree;
}

public void setCoefficients(double[] coeffs){
   this.coeffs = coeffs;
}


public Polynomial() {
    p = 0;

}


public Polynomial(double[] c) throws IllegalArgumentException{
c = this.coeffs.clone();

if (c[0] == 0)  
   throw new IllegalArgumentException("Zero, try again");
if (c.length > 1)
    throw new IllegalArgumentException("Greater than 1");  
}





@Override
public double hornerEval(double x) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public double naiveEval(double x) {
    System.out.println(Arrays.toString(c));
    double y = coeffs[degree()];

     for(int k = degree()-1; k >= 0; k--)
     {
         p++;
         double t = 1;
         for(int j = 1; j <= p; j++)
         {
             t*=x;
         }
         y += coeffs[k]*t;
     }
     return y;

}

@Override
public int degree() {
    return this.degree;
}

@Override
public String toString() {
    if      (this.degree == -1) return "0";
    else if (this.degree ==  0) return "" + this.coeffs[0];
    else if (this.degree ==  1) return this.coeffs[0] + "x +" + this.coeffs[1];
    String s = this.coeffs[0] + "x^" + degree;
    for (int i = 1; i <= this.degree; i++) {        

        if      (this.coeffs[i] == 0) continue;
        else if (this.coeffs[i] == 1) s = s + " + ";
        else if (this.coeffs[i] == -1) s = s + " - ";
        else if (this.coeffs[i]  > 0) s = s + " + " + ( this.coeffs[i]);
        else if (this.coeffs[i]  < 0) s = s + " - " + (-this.coeffs[i]);
        if      (i == this.degree-1) s = s + "x";
        else if (i == this.degree) s = s;
        else if (i >=  1) s = s + "x^" + (this.degree-i);
    }
    s = s.replaceAll("([0-9])\\.0+([^0-9]|$)", "$1$2");
    return s;


}

}

1 个答案:

答案 0 :(得分:-1)

public Polynomial(double[] c)是您方法的构造函数。

构造函数中的第一个语句是:

 c = this.coeffs.clone();

这意味着您将作为constructors参数传入的变量c中当前保存的值替换为this.coeffs.clone()

返回的值

由于您的代码示例未显示字段coeffs的声明位置和方式,因此我假设它尚未初始化。

  

coeffs在另一个类中定义并初始化。如果我打印出coeffs我得到了数组,但是当我尝试将coeffs复制到c时,c不会从该方法打印出数组。 - 戴尔

根据您提供的示例,这必须是您未展示的Polynomial超类

您也未能提供Minimal, Complete, and Verifiable example (MCVE)

因此,我认为打印'coeffs'的“检查”可能有效,因为它是通过直接实例化超类来完成的,这不是有效的证明。

然而...

忽略构造函数参数并立即替换它的值本身就是一个错误,也是我视图中问题的根本原因。

相关问题