gpt4 book ai didi

java - 使数组/矩阵可供程序中的其他方法使用。

转载 作者:行者123 更新时间:2023-12-01 15:01:23 25 4
gpt4 key购买 nike

我正在处理一些代码,这些代码读取 CSV 文件并将其转储到数组中。然后我需要以各种方法使用这些数据来绘制一些令人兴奋的图形。

但是在下面的代码中,当我尝试运行 glyph 方法时,我收到一个异常,即程序找不到任何称为 DSA 的内容。有人能指出我正确的方向吗?有人告诉我应该在定义字符串之前添加“public”,但这只会导致另一个错误(意外的标记)。

  void setup() {

cp5 = new ControlP5(this);
cp5.addButton("Overview")
.setValue(0)
.setPosition(840, 10)
.setSize(100, 19);

cp5.addButton("Quadrant")
.setValue(0)
.setPosition(840, 30)
.setSize(100, 19);

cp5.addButton("Location Map")
.setValue(0)
.setPosition(840, 50)
.setSize(100, 19);

String [][] DSA = readFile("DSA.csv");
String [][] NC = readFile("NC.csv");
String [][] IW = readFile("IW.csv");

size(950, 600);
smooth();
//noStroke();
//Use system font 'Arial' as the header font with 12 point type
h1 = createFont("Arial", 12, false);
//Use system font 'Arial' as the label font with 9 point type
l1 = createFont("Arial", 9, false);

}
String [][] readFile(String fileName) {
//for importing csv files into a 2d array
//by che-wei wang

String lines[] = loadStrings(fileName);
String [][] csv;
int csvWidth=0;

//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
String [] chars=split(lines[i], ',');
if (chars.length>csvWidth) {
csvWidth=chars.length;
}
}

//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];

//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], ',');
for (int j=0; j < temp.length; j++) {
csv[i][j]=temp[j];
}
}
return csv;
}

void Gluph() {
println(DSA[1][3])
}

最佳答案

这是一个范围界定问题。了解变量作用域。

一种解决方案是您可以在全局空间中声明变量。现在它们是在 setup() 函数中声明的,一旦 setup() 退出,这些变量就不再可供其他函数使用,因为它们是在 setup() 范围内声明的。如果您在程序的第一行 setup() 之前声明它们,您将可以在全局范围内访问它们。

如果您仍然需要在 setup 中 readFile(),那么只需在全局范围内声明变量并在 setup() 中赋值即可。由于变量是在全局作用域中声明的,因此无论您从何处访问它们,setup() 作用域中的更改仍然会反射(reflect)出来。

String [][] DSA;
String [][] NC;
String [][] IW;

void setup() {

cp5 = new ControlP5(this);
cp5.addButton("Overview")
.setValue(0)
.setPosition(840, 10)
.setSize(100, 19);

cp5.addButton("Quadrant")
.setValue(0)
.setPosition(840, 30)
.setSize(100, 19);

cp5.addButton("Location Map")
.setValue(0)
.setPosition(840, 50)
.setSize(100, 19);

DSA = readFile("DSA.csv");
NC = readFile("NC.csv");
IW = readFile("IW.csv");

size(950, 600);
smooth();
//noStroke();
//Use system font 'Arial' as the header font with 12 point type
h1 = createFont("Arial", 12, false);
//Use system font 'Arial' as the label font with 9 point type
l1 = createFont("Arial", 9, false);

}
String [][] readFile(String fileName) {
//for importing csv files into a 2d array
//by che-wei wang

String lines[] = loadStrings(fileName);
String [][] csv;
int csvWidth=0;

//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
String [] chars=split(lines[i], ',');
if (chars.length>csvWidth) {
csvWidth=chars.length;
}
}

//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];

//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], ',');
for (int j=0; j < temp.length; j++) {
csv[i][j]=temp[j];
}
}
return csv;
}

void Gluph() {
println(DSA[1][3])
}

关于java - 使数组/矩阵可供程序中的其他方法使用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593677/

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