有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计算在论坛上回复答案所需的时间

嗨,我在大学有一个小组项目。我和我的团队成员需要创建一个网站。 我们已经做到了,今年我们学习了Java编程语言。我的讲师给了我们另一项任务,使用Java,尤其是使用JSP(JavaServer Pages),将我们的网站从静态升级到动态

我负责为我们的网站为这项新任务创建一份统计报告。到目前为止,我能够从互联网上找到如何计算均值、总和和中位数,尤其是在这个论坛上。但是我无法找到如何根据字符串数组列表计算管理员回答用户问题所需的时间

有人能帮我吗?我对这个Java非常陌生,任何帮助都将不胜感激

我很抱歉我的语法,英语不是我的主要语言。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用线程保持时间:

    public class KeepingTime
    {
        private volatile int timeTaken;
        private Thread timekeeper;
    
        public KeepingTime()
        {
            timekeeper = new Thread(new Runnable(){
                public void run()
                {
                    try
                    {
                        while(true)
                        {
                            Thread.sleep(1000);
                            timeTaken++;
                            if(Thread.interrupted())
                                return;
                        }
                    }
                    catch(InterruptedException e)
                    {
                        return;
                    }
                }    //end run
            }); //end thread
            timekeeper.start();
        }    //end constructor
    
        public void stop()
        {
            timekeeper.interrupt();
        }
    
        public int timeTaken()
        {
            return timeTaken;
        }
    }
    

    然后像这样使用:

    for(String question : questions)
    {
        KeepingTime timer = new KeepingTime();
        //ask the question
        //wait until it is answered
        timer.stop();
        int timeTaken = timer.timeTaken();
    }
    

    请注意,我没有使用线程。停止(),因为它已被弃用。如果运行大量线程,这可能会不一致