令牌“]”上的语法错误,此令牌后预期的VariableDeclaratorld

时间:2021-11-29 19:12:07

I am trying to pass thistime[] to class and use it to define width and height of two rects.This is just a simplified version of my initial code and I get the errorsyntax error on token "]", VariableDeclaratorld expected after this token", here are my code:

我试图将thistime []传递给类并使用它来定义两个rects的宽度和高度。这只是我的初始代码的简化版本,我在令牌“]”上得到了错误的语法错误,在此令牌之后期望VariableDeclaratorld“ ,这是我的代码:

ArrayList textlines;

int xpos=20;
int ypos=20;
int[]thistime = new int[2];

void setup() {
  size(1200, 768);
  textlines = new ArrayList();
  thistime[0] =3;
  thistime[1] =30;
}

void draw() {
}


void mousePressed() {
  textlines.add(new Line(xpos, ypos,thistime));
  for (int i=0; i<textlines.size(); i++) {

    Line p=(Line)textlines.get(i);
    p.display();
  }
}


class Line {

  int x;
  int y;
  int thatimee[];

  Line(int xpo, int ypo, int thetimee[]) {
    x =xpo;
    y =ypo;
    thatimee[]= new int[thetimee.length];
    thatimee[0]=thetimee[0];
    thatimee[1]=thetimee[1];
  }

  void display() {
    fill(50, 50, 50);
    rect(random(width), random(height), thatimee[0],thatimee[0] );
    rect(random(width), random(height), thatimee[1], thatimee[1]);
  }
}

The error is in the line

错误在行中

thatimee[]= new int[thetimee.length];

Who knows the reason?

谁知道原因?

4 个解决方案

#1


2  

Line(int xpo, int ypo, int thetimee[]) {
    x = xpo;
    y = ypo;
    thatimee = new int[thetimee.length];
    thatimee[0] = thetimee[0];
    thatimee[1] = thetimee[1]; 
}

You already declared the variable "thatimee" as an array up there, remove the "[]" within the scope of Line when initializing the variable.

您已将变量“thatimee”声明为数组,在初始化变量时删除Line范围内的“[]”。

#2


2  

Try removing the [] in the assignment. like this:

尝试删除作业中的[]。喜欢这个:

thattimee = new int[thetimee.length];

#3


2  

just use

只是用

thatimee = new int[thetimee.length];

the [] is for declaring an array. It should not be used while initializing.

[]用于声明数组。初始化时不应使用它。

#4


2  

You can't put thatimee[] when initializing the array. You simply put:

初始化数组时,不能放置thatimee []。你简单地说:

thatimee = new int[thetimee.length];

thatimee represents the handle of the array, and you are storing something in the handle.

thatimee表示数组的句柄,并且您正在句柄中存储内容。

#1


2  

Line(int xpo, int ypo, int thetimee[]) {
    x = xpo;
    y = ypo;
    thatimee = new int[thetimee.length];
    thatimee[0] = thetimee[0];
    thatimee[1] = thetimee[1]; 
}

You already declared the variable "thatimee" as an array up there, remove the "[]" within the scope of Line when initializing the variable.

您已将变量“thatimee”声明为数组,在初始化变量时删除Line范围内的“[]”。

#2


2  

Try removing the [] in the assignment. like this:

尝试删除作业中的[]。喜欢这个:

thattimee = new int[thetimee.length];

#3


2  

just use

只是用

thatimee = new int[thetimee.length];

the [] is for declaring an array. It should not be used while initializing.

[]用于声明数组。初始化时不应使用它。

#4


2  

You can't put thatimee[] when initializing the array. You simply put:

初始化数组时,不能放置thatimee []。你简单地说:

thatimee = new int[thetimee.length];

thatimee represents the handle of the array, and you are storing something in the handle.

thatimee表示数组的句柄,并且您正在句柄中存储内容。