我正在尝试在处理中制作图表,但不断收到“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/