我試圖讓這個小窗口窗體應用程序,讓你點擊一個按鈕,它會計算你每秒點擊多少。但是,我收到不想要的結果。獲得一個列表的平均值
標籤總是爲0或1.我的工作是使用Stopwatch類作爲計時器。
public List<long> clickTimes = new List<long>();
Stopwatch stopwatch = new Stopwatch();
long last = 0;
private void startCalc()
{
stopwatch.Start();
}
private void stopCalc()
{
stopwatch.Stop();
}
private void resetCalc()
{
stopwatch.Reset();
}
private long time()
{
return stopwatch.ElapsedMilliseconds;
}
private bool isOn()
{
return stopwatch.IsRunning;
}
private long frequencyFromInterval(long ms)
{
return 1000/ms;
}
private void button1_Click(object sender, EventArgs e)
{
if (isOn() == false)
{
startCalc();
}
last = time() - last;
clickTimes.Add(last);
}
private void button2_Click(object sender, EventArgs e)
{
long avg = 0;
for (int i = 0; i < clickTimes.Count; i++)
{
avg = avg + clickTimes[i];
}
avg = avg/clickTimes.Count;
avg = frequencyFromInterval(avg);
label1.Text = "Avg. CpS: " + avg.ToString());
}
'return 1000L/ms;' – Damith
從簡單的設計角度來看,您可以使用Rx快速實現此目的。 – Aron
@Damith長整數仍然會在小數點後的部分數字下降。 –