本文介绍了在 Processing 中从 TXT 文件中读取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理中有以下程序,我试图找到一种方法来从 TXT 文件中读取变量的值.

I have the following program in Proccessing and I try to find a way to read the values ​​of variables from a TXT file.

static final int ribbon_length = 255, H = 200;

void setup() {
  size(ribbon_length, H); //Διαστάσεις παλέτας
}

void draw() {
  float p = 1;
  int up_y = 10;
  int widthh = 1;
  int height = 180;
  float a = pow (ribbon_length, 1-p);
  float colour = 0;
  for (int step = 0; step <= 255; step++) {
      colour = a * pow (step, p);
      fill(colour,0,0);
      rect(widthh*step, up_y, widthh, height);
      noStroke();
   }
}

我想从 txt 中读取的值是

The values tha I want to read from the txt is

  float p = 1;
  int up_y = 10;
  int widthh = 1;
  int height = 180;

我找到了 BufferedReader 命令,但我不确定它是否是我想要的.我尝试 ρθν 一个例子..但它没有用......

I found BufferedReader command but I'm not sure if it is thaτ I want. I try ρθν an example..but it didn't work...

BufferedReader reader;
String line;
static final int ribbon_length = 255, H = 200;

void setup() {
  size(ribbon_length, H);
  reader = createReader("positions.txt");
}
.
.
.
.

有什么想法吗????

感谢您的回答.在您的推荐后,我尝试了一些更改.但它没有显示任何颜色.

Thanks for your answers.I try some changes after your commends. But It didn't show any color.

static final int ribbon_length = 255, H = 200;

void setup() {
  size(ribbon_length, H);
}

void draw() {
  String[] lines = loadStrings("input.txt");
  float p = float(split(lines[0], "=")[1]);
  int up_y = int(split(lines[1], "=")[1]);
  int wide = int(split(lines[2], "=")[1]);
  int high = int(split(lines[3], "=")[1]);
  float a = pow (ribbon_length, 1-p);
  float colour = 0;
  for (int step = 0; step <= 255; step++) {
      colour = a * pow (step, p);
      fill(colour,0,0);
      rect(wide*step, up_y, wide, high);
      noStroke();
   }
}

推荐答案

首先:将变量命名为 widthheight 是个坏主意,因为这些是内置于的特殊变量加工.你应该使用不同的东西,比如 widehigh,也许.

First: it's a bad idea to name variables width and height because these are special variables built into Processing. You should use something different, like wide and high, perhaps.

最简单的方法是拥有一个像这样的纯文本文件:

The simplest way to do it is to have a bare text file like this:

1
10
1
180

正如我所写的那样.然后,您可以使用 Processing 的内置函数读取这些值.这些是 xp500 答案提供的更简单的替代方案,这是纯 Java 的正确答案.

exactly as I have written it. You can then use the built-in functions of Processing to read in these values. These are easier alternatives to what xp500's answer provides, which is the correct answer for pure java.

void setup(){
  ....
  String[] lines = loadStrings("positions.txt");
  float p = float(lines[0]);
  int up_y = int(lines[1]);
  int wide = int(lines[2]);
  int high = int(lines[3]);
  ....
}

这会起作用,但不是很容易维护,因为有很多硬编码值.如果文本文件顶部有一个额外的空行怎么办?第一行 (lines[0]) 不会有 p 的值,第二行 (lines[1]) 会有有 p 的值而不是 up_y 的值!这只是需要记住的事情.如果您最终在文本文件中包含其他内容(例如变量名称),您可以使用 split() 从字符串中获取数字.示例:

This will work, but is not very maintainable because there are lots of hard-coded values. What if there's an extra empty line at the top of the text file? The first line (lines[0]) won't have p's value, and the second line (lines[1]) will have p's values instead of up_y's! This is just something to keep in mind. If you end up having other things in your text file (such as variable names), you could use split() to get the number out of the string. Example:

文本文件:

p=1
up_y=10

处理代码:

String[] lines = loadStrings("input.txt")
float p = float(split(lines[0], "=")[1]); // split on the "=" and take the second element
int up_y = int(split(lines[1], "=")[1]);

如果您选择这种方法,您甚至可以在分配之前检查该行中的变量名,以防万一文件混乱.类似于 if(!lines[0].contains("p")) print("error!");

If you choose this approach, you might even check for the variable name in the line before assigning it, just in case the file is jumbled. Something like if(!lines[0].contains("p")) print("error!");

这篇关于在 Processing 中从 TXT 文件中读取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:45