0

我正在尝试将 thistime[] 传递给类并使用它来定义两个矩形的宽度和高度。这只是我的初始代码的简化版本,我在令牌“]”上得到了错误语法错误,在这个令牌之后应该是 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]);
  }
}

错误在行

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

谁知道原因?

4

4 回答 4

2

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

thatimee = new int[thetimee.length];

thatimee表示数组的句柄,并且您在句柄中存储了一些东西。

于 2013-06-23T02:30:17.793 回答
2

尝试删除作业中的 []。像这样:

thattimee = new int[thetimee.length];
于 2013-06-23T02:29:08.963 回答
2

只需使用

thatimee = new int[thetimee.length];

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

于 2013-06-23T02:29:27.543 回答
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]; 
}

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

于 2013-06-23T03:56:18.507 回答