在我的主要课程中,我有以下代码:
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)