randomCounts的目的是什么[index] ++;?

时间:2014-05-16 14:03:24

标签: java processing

我对Java编程很陌生,实际上正在研究Daniel Shiffman的随机分布代码示例。

我能知道randomCounts [index] ++的目的是什么? ?它说反击它实际上做了什么?每次函数draw()运行时它会有什么价值?

  1. 我知道randomCounts有20个存储空间。
  2. index始终是随机的,值介于0 - 20之间。
  3. 但只是不了解randomCounts [index] ++。

        // An array to keep track of how often random numbers are picked
        float[] randomCounts;
    
        void setup() {
        size(800,200);
        randomCounts = new float[20];
        }
    
        void draw() {
        background(255);
    
        // Pick a random number and increase the count
        int index = int(random(randomCounts.length));
        randomCounts[index]++;
    
        // Draw a rectangle to graph results
        stroke(0);
        strokeWeight(2);
        fill(127);
    
        int w = width/randomCounts.length;
    
        for (int x = 0; x < randomCounts.length; x++) {
        rect(x*w,height-randomCounts[x],w-1,randomCounts[x]);
        } 
        }
    

2 个答案:

答案 0 :(得分:2)

++运算符递增变量。因此,在您的示例中,randomCounts[index]++;会增加index数组中索引randomCounts的浮点数。

请参阅:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

答案 1 :(得分:1)

您的回答位于声明正上方的代码注释中。

    randomCounts[index]++;

将计数加1。 index是数组floatrandomCounts的位置,也就是递增的位置。