为什么我的'如果'声明没有在此代码中执行? (处理)对

时间:2017-04-28 12:10:18

标签: conditional processing

    void setup(){
  size(800,600);
  background(0);
  fill(255,40,40);
  noStroke();
  ellipse(400,80,20,20);
  ellipse(150,513,20,20);
  ellipse(650,513,20,20);
  fill(255);
  frameRate(10);
}

float traceX = 350;
float traceY = 350;
String vertexPick = "";
float rand = 0;

void draw(){

  point(traceX,traceY);
   rand = random(1);
  if (rand > 2/3) {
    vertexPick = "A";
  }
  else if (rand < 1/3) {
    vertexPick = "B";
  } else {
    vertexPick = "C";
  }

  if (vertexPick == "A") {
    traceX = (traceX+400)/2;
    traceY = (traceY+80)/2;
  }
  else if (vertexPick == "B") {
    traceX = (traceX+150)/2;
    traceY = (traceY+513)/2;
  }
  else 
  {
    traceX = (traceX+650)/2;
    traceY = (traceY+513)/2;
  }  
}

我已经检查过了,每次通过“抽奖”#39;函数它生成一个介于0和1之间的新随机数。但是不是根据数字将vertexPick分配给一个新的字母,它会被卡在vertexPick的一个值上并永远保留在那里。谁能告诉我为什么?为什么不在每个循环上再次执行if语句?

1 个答案:

答案 0 :(得分:0)

整数除法存在问题,即2/30且条件rand > 2/3始终为真。

要使浮点除法使用2.0/3代替2/3

(请参阅此问题Division of integers in Java