我正在设计的应用程序的一部分要求用户点击一个按钮十次,然后根据他们点击的速率,返回平均BPM(每分钟心跳)。我注意到,在我点击鼠标和模拟器显示点击按钮之间似乎有延迟,但我不确定这是否意味着什么。
问题是它给我的BPM远远不够准确。如果我是正确的,那么BPM应该等于((10次点击按钮)*(一分钟内60秒)/(点击按钮10次所需的秒数),但这显然不适用于我的代码。奇怪的是,比60准确得多的倍数一直是52,我不明白为什么。
如果是编码错误,下面是附加的方法:
public void buttonOnClick(View v){
Button button=(Button) v;
TextView counts = (TextView)findViewById(R.id.textView);
TextView bpmText = (TextView)findViewById(R.id.textView2);
if(total==10){
startTime = System.nanoTime();
}
total=total-1;
counts.setText(total+"");
if(total == 0){
long elapseTime = System.nanoTime() - startTime;
double secs = elapseTime/1000000000.0;
counts.setText("");
button.setEnabled(false);
button.setText("");
bpm = (int)((10*BPM_CONSTANT)/secs);
bpmText.setText(bpm+"");
}
}
BPM_CONSTANT是倍数,我认为应该是60。
是电脑,还是我的逻辑?或者我的点击不准确?感谢反馈,谢谢。
将bpm计算结果强制转换为int
会导致更多的精度损失。请改用(int)Math.round(double)
。