Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算运行平均值的逻辑错误_Java_Android_Math_Sharedpreferences_Average - Fatal编程技术网

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

Java 计算运行平均值的逻辑错误,java,android,math,sharedpreferences,average,Java,Android,Math,Sharedpreferences,Average,我试图将平均值保持在SharedReference的范围内。这是我的密码: //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(Captu

我试图将平均值保持在SharedReference的范围内。这是我的密码:

//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);

我注意到您的代码中存在一个潜在的缺陷-以下几行是罪魁祸首:

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

让我们考虑一个场景,你有3个分数:99,98,90

由于您正在进行迭代平均运算,结果如下所示:

  • (99+0)/1=99----这很好
  • (99+98)/2=98.5--这很好
  • (98.5+90)/3=62.83---问题出在这里
相反,在第二次迭代之后,每次都应该除以2


解决这个问题的另一种方法是等到你收到所有的分数,然后将总分数除以捕获数。

要保持一个运行平均值,你不保持实际的平均值,你要保持运行总分数和样本数,然后用通常的方法计算平均值,即total/samples。因此,对于该集合,您应在每个阶段保留这些值

样本:9699100

总数:96,样本数:1=>AVG=96/1=96

总数:195,样本数:2=>AVG=195/2=97.5

总数:295,样本数:3=>AVG=295/3=98.333

其他地方指出的一种错误方法是,在第一个样本之后总是除以2,然后简单地将之前的平均值添加到新样本中。这将导致99个样本为100个,0个样本的平均值为50个,这显然是错误的

我会把你的代码改成这样

            int numberOfCaptures = prefs.getInt(CaptureActivity.NUMBER_OF_CAPTURES, 0);
            numberOfCaptures++;

            int runningTotal = prefs.getInt(CaptureActivity.RUNNING_TOTAL, 0);
            runningTotal += result.getBlastScore();

            //Calculate the average of all of the captures
            int averageBlastScore = runningTotal / 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.RUNNING_TOTAL, runningTotal).commit();
            prefs.edit().putInt(CaptureActivity.NUMBER_OF_CAPTURES, numberOfCaptures).commit();

如果您坚持存储运行平均值而不是运行总计,则必须先将该平均值乘以样本数,然后再增加样本数以获得上一个总计。

从数学上讲,您所说的运行平均值是什么意思?如果你想要一个移动平均值,那么你需要保留最后的
N
数据点,这样你就可以求和并除以N。如果你想要一个指数移动平均值,你可以做你现在正在做的事情,但是除以衰减因子
k
。我只想取平均值。我想我可能用错了术语。当我的应用程序继续运行时,我想取我的数据点的平均值。然后你需要保留最后的N个值,而不仅仅是之前的平均值。@SheehanAlam存储分数之和而不是平均值,并计算平均值,然后用分数之和除以捕获数显示