gpt4 book ai didi

java - 计算运行平均值的逻辑错误

转载 作者:行者123 更新时间:2023-12-01 15:41:52 25 4
gpt4 key购买 nike

我试图在 SharedPreference 内保持运行平均值。这是我的代码:

//Get the number of captures
int numberOfCaptures = prefs.getInt(CaptureActivity.NUMBER_OF_CAPTURES, 0);
numberOfCaptures++;

//Calculate the average of all of the captures
int runningAverage = prefs.getInt(CaptureActivity.AVERAGE_BLAST_SCORE, 0);
System.out.println("Running Average: " + runningAverage);

int averageBlastScore = (runningAverage + result.getBlastScore())/numberOfCaptures;

System.out.println("Blast Score: " + result.getBlastScore());
System.out.println("Number of Captures: " + numberOfCaptures);
System.out.println("Average Blast Score: " + averageBlastScore);

//Save it, so we can get it again if the user captures another swing
prefs.edit().putInt(CaptureActivity.AVERAGE_BLAST_SCORE, averageBlastScore).commit();
prefs.edit().putInt(CaptureActivity.NUMBER_OF_CAPTURES, numberOfCaptures).commit();

我的运行平均值似乎没有正确添加。

这是 3 次运行的结果:

10-28 02:53:13.690: I/System.out(1162): Running Average: 0
10-28 02:53:13.690: I/System.out(1162): Blast Score: 96
10-28 02:53:13.690: I/System.out(1162): Number of Captures: 1
10-28 02:53:13.690: I/System.out(1162): Average Blast Score: 96

10-28 02:53:25.550: I/System.out(1162): Running Average: 96
10-28 02:53:25.550: I/System.out(1162): Blast Score: 99
10-28 02:53:25.550: I/System.out(1162): Number of Captures: 2
10-28 02:53:25.550: I/System.out(1162): Average Blast Score: 97

10-28 02:54:04.720: I/System.out(1162): Running Average: 97
10-28 02:54:04.720: I/System.out(1162): Blast Score: 100
10-28 02:54:04.720: I/System.out(1162): Number of Captures: 3
10-28 02:54:04.720: I/System.out(1162): Average Blast Score: 65

到第三次运行时我应该:

Running Average: 295
Average Blast Score: 98.3

我不太确定我做错了什么。

最佳答案

看这一行:

int averageBlastScore = (runningAverage + result.getBlastScore())/numberOfCaptures;

您预计在第 100 次迭代之后会发生什么?

您应该通过将分数相加并除以捕获数量来找到平均值:

int sumBlastScore = prefs.getInt(CaptureActivity.SUM_BLAST_SCORE, 0) + result.getBlastScore();
int averageBlastScore = sumBlastScore/numberOfCaptures;

System.out.println("Running Average: " + averageBlastScore);

关于java - 计算运行平均值的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7924684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com