如何计算经过时间 [英] How to count elapsed time

查看:151
本文介绍了如何计算经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个计数器。我有10和20个问题的游戏。我需要算多少时间过去了,当用户完成比赛。

I have this counter below. I have a 10 and 20 questions game. I need to count how much time is passed when a user finish the game.

Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {         
                @Override
                public void run() {
                    runOnUiThread(new Runnable()
                    {

                        public void run()
                        {
                            countdown.setText(""+count);
                            count++;                
                        }
                    });
                }
            }, 1000, 1000);

我用它来停止计数器:

I use this to stop the counter:

T.cancel();

现在我需要两件事情。我需要的终值是一个双例如最后得分是:15,49秒。而第二件事是五言的方法来计算所经过的时间,并将其存储在一个变量。谢谢你。

Now I need two things. I need the final value to be a double, for example final score is: 15,49 seconds. And the second thing is of cource a way to count the elapsed time and store it in a variable. Thanks.

推荐答案

在游戏开始:

long tStart = System.currentTimeMillis();

在游戏结束:

long tEnd = System.currentTimeMillis();
long tDelta = tEnd - tStart;
double elapsedSeconds = tDelta / 1000.0;

这篇关于如何计算经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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