在mousePressed中,我需要aNumber的值,但是我不能传递mousePressed(in Number)..因此,当通过javascript调用Numbers时,我需要某种全局变量来保持修改状态。

int number = 0;
int theNumbers(int aNumber) { //aNumber = 1, 2, or 3, from the javascript)

  println(number); // prints the correct number
  int number = aNumber; // set the global variable number equal to aNumber
  return number;
}

void mousePressed() {
  aLongNumber = 10000000000;

  println(number); // prints 0 right now, should print the value of aNumber
  long numberLong = aLongNumber + (number * aLongNumber);
}

最佳答案

您在theNumbers()中声明局部变量,只需在int之前删除number即可,因此您的函数应如下所示:

int theNumbers(int aNumber) { //aNumber = 1, 2, or 3, from the javascript)

  println(number);
  number = aNumber; // removed int that made new local variable "number"
  return number;
}

关于javascript - 使用函数修改全局变量吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23887099/

10-10 22:23