【发布时间】:2017-06-15 23:20:37
【问题描述】:
我正在尝试向班级发送一个 D 数组。我希望 mutator 方法复制数组,保存它,然后将其发送回我的测试器类......到目前为止,我不断收到来自编译器的错误。我知道如何编写一个在 main 中复制数组的方法,但我似乎无法用类完成它。代码如下:
public class Class1D {
private int degree;
private int [] coefficient;
public Class1D(int degree){
this.degree =degree;
}
public void setCoefficent( int[] a, int degree){
this.coefficient[degree] = a[degree];
for ( int i=0; i<=a.length-1; i++)
{
this.coefficient[i] = a[i];
}
}
public int [] getCoefficient() {
return coefficient;
}
}
import javax.swing.JOptionPane;
public class copy1D{
public static void main(String[]args)
{
String input;
int degree;
input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
degree = Integer.parseInt(input);
degree= degree+1;
int [] array = new int[degree];
for ( int i =0; i<=array.length-1; i++)
{
input = JOptionPane.showInputDialog(" Enter coefficients:");
array[i] = Integer.parseInt(input);
}
for ( int i =0; i<=array.length-1; i++)
{
System.out.print(array[i] + " ");
}
Class1D array2 = new Class1D(degree);
}
}
}
【问题讨论】:
-
你说你从编译器得到错误 - 你能具体点吗?
-
它一直说类接口或枚举预期......我也不知道如何让一个类复制一个数组。请对新的计算机科学家有点同情......
-
如果我没有遗憾,我会忽略这个问题 :) 我注意到该代码中的第一行
public Class1D {似乎无效。不应该是public class Class1D {吗? -
是的,继续取笑我的多动症,我以某种方式修复了它
-
我真的不是在开玩笑。像这样的小事情会使整个事情无法编译。