在计算处理百分比时我做错了什么?

时间:2016-06-01 06:59:26

标签: processing

我正在编写一个简单的处理脚本,它执行一个随机布尔值,并计算它的1而不是0。

然后我添加了一个随机完成总量的计数器。 我想用它来计算1ns的百分比。 到目前为止一切正常。 然后我想做计算。无论我做什么,它都会显示0。 代码就像

bool r; //boolean for random
int t;  //t fr true or 1
int f;  //f for false or 0
int cnt; //cnt for total count
float p; //for the percentage
Void draw(){
 r = int(random(2));
 if (r==0){int(f++);}
 if (r==1){int(t++);}
 if (r<100){cnt++;}
 p = t / cnt * 100; //calculating percentage.
 text(p,10,100);   //draws text on sceen at x=10 and y=100 but it always draws 0
}

这有什么不对?我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为是这个问题:Why dividing two integers doesn't get a float?

所以你可以这样写:

(t * 100.0f)/cnt