0

在我的主要课程中,我有以下代码:

Polynomial P = new Polynomial(polynomial);

在另一个类中,我有以下代码:

public class Polynomial {

    private int[] polynomial;

    public Polynomial(int[] polynomial) {
        this.polynomial = polynomial;
    }
}

为什么构造函数多项式(int [])未定义?

顺便说一句......主类中的多项式指向:

int [] polynomial = new int[count];

这是完整的主要课程:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Arrays;
public class Main {

public static void main(String[] args) {
    String input = JOptionPane.showInputDialog(null, "This is the Polynomial Cal" +
            "culator Menu. Please ente" +
                "r your menu selection:\n (1) Enter c" +
                    "oefficients of polynomial P(x). \n (2) Enter co" +
                    "efficients of polynomial Q(x). \n (3) Sum polynomi" +
                        "als P(x) and Q(x). \n (4) Multiply polynomials P(x) and Q(" +
                            "x). \n (5) Quit.", "Polynomial Menu",JOptionPane.PLAIN_MESSAGE);
    Scanner inputScanner =new Scanner(input);           //Scanner for Menu
    int userChoice = inputScanner.nextInt();            //Menu Choice
    if(userChoice>=1 && userChoice<=5)                  //User Input Catch
    {
        switch(userChoice)
        {
        case 1: String coefficientInput= JOptionPane.showInputDialog(null, "Please enter th" +
                "e coefficients of the terms in the polynom" +
                    "ial.(Ax^n, Bx^(n-1)...Yx,Z) \n Only ent" +
                        "er the values of the coeffien" +
                            "ts i.e (A + B - C + D) ");
                Scanner countScanner = new Scanner(coefficientInput);       //Scanner for count
                int coefficient= countScanner.nextInt();
                int count=1;
                while(countScanner.hasNextInt())
                {
                    count++;
                    countScanner.nextInt();
                }
                int [] polynomial = new int[count];                         //Size of Array=Count
                Scanner coefficientScanner = new Scanner(coefficientInput);
                int term = 0;
                System.out.println(count);
                int i=0;
                while(coefficientScanner.hasNextInt())                                  //Initialisation of array
                {
                    term=coefficientScanner.nextInt();
                    polynomial[i]=term;
                    i++;
                }
                Polynomial P = new Polynomial(polynomial);

        }
    }
    else
    {
        JOptionPane.showMessageDialog(null, "No option selected. Please try again.","Input Error",JOptionPane.ERROR_MESSAGE);
    }

}

}

错误发生在多项式 P =new Polynomial(polynomial)

4

3 回答 3

2

我猜想主类中命名的引用polynomial并不指向int []. 如果它是类型的引用,Polynomial您要么必须创建另一个接受 a 的构造函数Polynomial(又名“复制构造函数”),要么更改polynomial.

我不喜欢您编写构造函数的方式。那样不是私人的。您传入的引用是可变的。制作防御副本。这是我的做法:

public class Polynomial {

    private int[] coefficients;

    public Polynomial(int[] coefficients) {
        if (coefficients == null) throw new IllegalArgumentException("coefficients cannot be null");
        this.coefficients = new int[coefficients.length];
        System.arraycopy(0, coefficients, 0, this.coefficients, this.coefficients.length);
    }

    public Polynomial(Polynomial p) {
        this(p.coefficients);
    }
}

这也是一种幼稚的设计。没有浮点系数?如果你想建模类似y = x^1000 + 1. 您将在一个非常大的数组中有两个非零系数。

更好的设计是创建一个MonomialPolynomial维护一个List

于 2013-03-12T12:23:47.307 回答
0

好的。我只是删除了一行代码并重新输入了它-然后它起作用了。刚刚发生了什么?

于 2013-03-12T12:55:40.530 回答
0

以下代码将愉快地编译,您的类定义和构造函数没有问题。

注意如何定义和初始化整数数组。

public static void main(String[] args){
    int[] polynomial = new int[]{2, 1, 2};
    Polynomial P = new Polynomial(polynomial);
}
于 2013-03-12T12:29:23.777 回答