gpt4 book ai didi

java - 处理映射总是越界

转载 作者:行者123 更新时间:2023-11-30 03:55:37 24 4
gpt4 key购买 nike

我正在尝试在处理中制作图表,但不断收到“ArrayIndexOutOfBounds”错误。我很难理解 map() 语法的概念,有人能为我解答吗?我不确定 map(x,y,z,a,b); 中的每个数字应该代表什么;

String[] name = {
"1st:",
"5th:",
"10th:",
"15th:",
"20th:",
"25th:",
"30th:"
};

int[] temperature = {
81,
82,
84,
85,
87,
88,
90
};


void setup(){
size(200,200);
}

void draw(){
int x=0;
for(int i=0;i<10;i++){

if(mouseX>x && mouseX<=x+40){
fill(255,40,40);
}else{
fill(50);
}

float h = map(temperature[i], 0, 100, 0, 200);

rect(x+4,height-h,32,h);

x+=40;
}

}

最佳答案

您的数组有 7 个元素,但您迭代了 10 次。

for(int i=0;i<10;i++){
...
float h = map(temperature[i], 0, 100, 0, 200);

}

要么用额外的 3 个元素填充数组,要么更好:您应该使用 i < temperature.length() 而不是数字 10。作为 for 循环中的条件。

关于java - 处理映射总是越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273278/

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