gpt4 book ai didi

java - 安卓/Java : Code to calculate the average of values in an arrayList?

转载 作者:行者123 更新时间:2023-11-29 07:47:07 24 4
gpt4 key购买 nike

在我的应用程序中,我正在从 EEG 耳机读取外部值 (int)。

我认为该值每秒读取一次,但从我的 logcat 报告来看,它似乎被读取的次数不止于此。

这打乱了我的 Intent ,因为我想计算这些值的平均值。我正在尝试使用一个名为 count 的增量运算符来做到这一点。

请注意,从耳机读取的值只能是 1-100,因此平均值的输出必须是不正确的,因为它是 168。

我做错了什么?

当前代码:

记录值(value)的地方:

case TGDevice.MSG_MEDITATION:

//BELOW CODE RELATES TO SAVING AN AVERAGE VALUE

meditationValues.add(msg.arg1);
count++;

for(int i = 0; i < meditationValues.size(); i++){

totalofMedLevels += meditationValues.get(i);

Log.d("Total", "Total : " + totalofMedLevels);
Log.d("Count ", "Count: " + count);

}

计算平均值:

public void getAverageValue(){
//stop device so that the values can be recorded
device.stop();


averageMedLevel=totalofMedLevels/count;

Log.d("Average level:", "Average: " + averageMedLevel);

}

Logcat 输出:

总计:

07-14 14:16:44.312: D/Total(30422): Total : 301
07-14 14:16:45.313: D/Total(30422): Total : 301
07-14 14:16:45.313: D/Total(30422): Total : 301
07-14 14:16:45.313: D/Total(30422): Total : 301
07-14 14:16:45.323: D/Total(30422): Total : 301
07-14 14:16:45.323: D/Total(30422): Total : 301
07-14 14:16:45.323: D/Total(30422): Total : 301
07-14 14:16:45.323: D/Total(30422): Total : 342
07-14 14:16:45.323: D/Total(30422): Total : 406
07-14 14:16:45.323: D/Total(30422): Total : 456
07-14 14:16:45.323: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.314: D/Total(30422): Total : 496
07-14 14:16:46.324: D/Total(30422): Total : 537
07-14 14:16:46.324: D/Total(30422): Total : 601
07-14 14:16:46.324: D/Total(30422): Total : 651
07-14 14:16:46.324: D/Total(30422): Total : 691
07-14 14:16:46.324: D/Total(30422): Total : 725
07-14 14:16:47.285: D/Total(30422): Total : 725
07-14 14:16:47.285: D/Total(30422): Total : 725
07-14 14:16:47.285: D/Total(30422): Total : 725
07-14 14:16:47.285: D/Total(30422): Total : 725
07-14 14:16:47.285: D/Total(30422): Total : 725

计数:

07-14 14:16:47.285: D/Count(30422): Count: 12
07-14 14:16:47.285: D/Count(30422): Count: 12
07-14 14:16:47.285: D/Count(30422): Count: 12
07-14 14:16:47.285: D/Count(30422): Count: 12
07-14 14:16:47.285: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:47.295: D/Count(30422): Count: 12
07-14 14:16:48.286: D/Count(30422): Count: 13
07-14 14:16:48.286: D/Count(30422): Count: 13
07-14 14:16:48.286: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13
07-14 14:16:48.296: D/Count(30422): Count: 13

对于平均值:

07-14 14:16:53.281: D/Average level:(30422): Average: 168

编辑(尝试仅保存大于 0 的值):

if((msg.arg1)!=0){

meditationValues.add(msg.arg1);

}



for(int i = 0; i < meditationValues.size(); i++){

//totalofMedLevels += meditationValues.get(i);
totalofMedLevels+=msg.arg1;
count++;

Log.d("Total", "Total : " + totalofMedLevels);
Log.d("Count ", "Count: " + count);

}

最佳答案

似乎每次获得新值时,您都会遍历整个值列表并将它们再次添加到“totalofMedLevels”。将上面的代码替换为

totalofMedLevels += msg.arg1;
count++;

应该可以解决问题。如果不是,则说明您没有提供足够的信息(即,显示的代码足够多)。

关于java - 安卓/Java : Code to calculate the average of values in an arrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24737556/

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