计算变量值的平均值,机器人 [英] Calculating the average of a Variable value, android

查看:203
本文介绍了计算变量值的平均值,机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这取决于用户的速度产生的价值每一秒后,code。现在我想由用户生成的所有速度值的平均值。我想在一个TextView来显示它。

I have a code in which the value is generated after every second depending on the users speed. Now i want the average values of all the generated speed values by the user. I want to display it in a textview.

我怎么能做到这一点,任何帮助?

How can i do it, any help?

下面是我的code:

LocationManager lm = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    this.onLocationChanged(null);

}

@Override
public void onLocationChanged(Location location) {
    TextView tv = (TextView) findViewById(R.id.textView1);

    if (location == null) {

        tv.setText("0.0");
    } else {

        nCurrentSpeed = location.getSpeed();
        tv.setText(nCurrentSpeed + "");

    }

现在这里nCurrentSpeed​​是产生每秒的价值,我怎样才能得到它的所有生成和有争议地产生所有这些的平均数值。

Now here nCurrentSpeed is the value generated every second, How can i get all the values that it generates and generate the average of all these contentiously.

推荐答案

你为什么不添加2个变量到你的活动?

Why don't you add 2 variables to your Activity?

private long startTime;
private float pointAverage;

Initalize在活动这两个的onCreate()方法。

现在,您可以修改code

Now, you can modify your code

if (location == null) {
        startTime = System.currentTimeMillis();
        pointAverage = 0;
        actualizeTextField();
}

(这是你的开始,不是吗?)

(This is your Start, right?)

else {
        pointAverage += location.getSpeed();
        actualizeTextField();
}

最后补充像这样

    public void actualizeTextField() {
        TextView tf = (TextView) findViewById(R.id.textView1);

        if(count > 0) {
            long timeOver = System.currentTimeMillis() - startTime;
            tf.setText(String.valueOf(pointAverage/(timeOver/1000)));
        } else {
            tf.setText("0.0");
        }
    }

这篇关于计算变量值的平均值,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆