0

我有一些正在处理的代码,它读入一个 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])
}
4

2 回答 2

1

这是一个范围界定问题。阅读有关变量范围的信息。

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

如果您仍需要在 setup 中读取 File(),则只需在全局范围内声明变量并在 setup() 中赋值。由于变量是在全局范围内声明的,因此无论您从何处访问它们,setup() 范围内的更改仍会反映出来。

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])
}
于 2012-11-29T21:14:00.277 回答
0

您想让 DSA 成为类变量。我在这里看不到类定义,但它会是这样的:

class blah{
  String [][] DSA;

  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");
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);

}

}
于 2012-11-27T21:48:45.667 回答