当我尝试将参数传递给构造函数(即创建对象)时,为什么会出现红色下划线错误?我做错了什么?
public static void main(String[] args)
{
CreateShape temp = new CreateShape(3,3, 'a',
{{'x','.','.'}
{'.','.','x'}
{'x','.','x'}}, "x . .\n"
+ ". . x\n"
+ "x . x");
temp.rotateCW();
System.out.println(temp);
public CreateShape(int height, int width, char dc, char[][] charLayout, String layout)
{
this.height = height;
this.width = width;
this.dc = dc;
this.shape = charLayout;
this.layout = layout;
initialPos = Rotation.CW0;
}
通过为char[][]
编写参数,我犯了错误。
答案 0 :(得分:1)
我假设方法rotateCW和字段都在类中声明。
定义二维数组时,数组将作为数组的数组读取。在一维数组中,我们使用{entry,entry}。类似地,在2D数组中,{{entry,entry},{entry,entry}}。此外,数组是一个对象,必须如此构造。
您的问题是您没有数组构造函数,并且数组之间没有逗号...所以数组应该定义为:
new char[][]{
{'x','.','.'},
{'.','.','x'},
{'x','.','x'}}
然后像往常一样继续其他论点。
答案 1 :(得分:0)
char数组。我觉得这对你有用
CreateShape temp = new CreateShape(3,3, 'a',
new char[][]{{'x','.','.'},
{'.','.','x'},
{'x','.','x'}}, "x . .\n"
+ ". . x\n"
+ "x . x");