我試圖在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
我不太確定我在做什麼錯。
從數學角度來說,你的意思是平均跑步數?如果你想要一個移動平均線,那麼你需要保留最後的'N'個數據點,以便將它們相加並將它們除以N.如果你想要一個指數移動平均線,你可以做你現在正在做的事情,由'k'表示衰減因子。 – ObscureRobot
我只想取平均值。我想我可能會使用錯誤的術語。隨着我的應用程序繼續運行,我想要獲取我的數據點的平均值。 –
然後,您將需要保留最後的N個值,而不僅僅是以前的平均值。 – ObscureRobot