gpt4 book ai didi

javascript - 用函数修改全局变量?

转载 作者:行者123 更新时间:2023-11-28 19:48:23 26 4
gpt4 key购买 nike

在 mousePressed 中,我需要 aNumber 的值,但我无法传递 mousePressed(int aNumber).. 因此,当通过 javascript 调用 theNumbers 时,我需要某种全局变量来保持修改状态..

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() 中声明局部变量,只需删除 number 之前的 int 即可,这样您的函数将如下所示:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com