将当前时间保存为变量并从该值继续计数

多伊

我正在制作秒表并努力使停止功能正常工作。

当我在 7 秒停止计时器并重新启动时,它会回到 0 秒并重新开始;但是,我希望它从 7 秒开始。有时,秒表会中断并持续显示 1.529679177849E9 秒。

我知道我需要将保存的时间存储在一个变量中,但我不知道如何。

你能帮我解决这个问题吗?

public class stopwatch {
    public long thetime = 0;
    public long stoppedtime = 0;
    public boolean ticking = false;


    public static void main(String[] args) {
        stopwatch s = new stopwatch();
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop = true) {
            System.out.println("1 start 2 is started 3 stop 4 "
                    + "reset 5 check time 6 stop");
            int i = sc.nextInt();
            if (i == 1) {
                s.start();
            } else if (i == 2) {
                System.out.println(s.isStarted());
            } else if (i == 3) {
                s.stop();
            } else if (i == 4) {
                s.reset();
            } else if (i == 5) {
                System.out.println("saved time is " + s.time() + " Seconds");
            } else if (i == 6) {
                System.out.println("closing");
                loop = false;
                break;
            } else {
                System.out.println("invalid");
            }

        }

    }

    public void start() {

        if (ticking == true) {
            thetime = thetime;
        } else {
            thetime = System.currentTimeMillis();
            ticking = true;
        }
    }

    public boolean isStarted() {
        return ticking;
    }

    public void stop() {
        if (ticking == false) {
            stoppedtime = stoppedtime;
        } else {
            stoppedtime = thetime;
            ticking = false;
        }
    }


    public void reset() {
        thetime = 0;
        stoppedtime = 0;
    }

    public double time() {
        double seconds = 1000.000000;
        double currenttime = 0;
        double saved = stoppedtime;
        if (ticking == true) {
            currenttime = ((System.currentTimeMillis() - thetime) / seconds);
            return currenttime;


        } else {
            currenttime = (stoppedtime / seconds);
            return currenttime;
        }

    }
}
阿曼查布拉

我已经修复了一些问题,请在下面找到工作代码:

1)停止时间在返回时间时没有转换成秒

2) 只需在实际结果中添加停止时间,以确保在调用重置之前始终添加它

3)根据编码标准,类名应始终以大写字母开头

public class Stopwatch {
public long thetime = 0;
public long stoppedtime = 0;
public boolean ticking = false;
public int laps = 0;
public long lastTime = 0;

public static void main(String[] args) {
    Stopwatch s = new Stopwatch();
    Scanner sc = new Scanner(System.in);
    boolean loop = true;
    while (loop = true) {
        System.out.println("1 start 2 is started 3 stop 4 " + "reset 5 check time 6 stop");
        int i = sc.nextInt();
        if (i == 1) {
            s.start();
        } else if (i == 2) {
            System.out.println(s.isStarted());
        } else if (i == 3) {
            s.stop();
        } else if (i == 4) {
            s.reset();
        } else if (i == 5) {
            System.out.println("saved time is " + s.time() + " Seconds");
        } else if (i == 6) {
            System.out.println("closing");
            loop = false;
            break;
        } else {
            System.out.println("invalid");
        }

    }

}

public void start() {

    if (!ticking) {
        thetime = System.currentTimeMillis();
        ticking = true;
    }
}

public boolean isStarted() {
    return ticking;
}

public void stop() {
    if (ticking ) {
        stoppedtime += (System.currentTimeMillis() - thetime);
        ticking = false;
    }
}

public void reset() {
    thetime = System.currentTimeMillis();
    stoppedtime = 0;
}

public double time() {
    double seconds = 1000.000000;
    double currenttime = 0;
    if (ticking) {
        currenttime = (stoppedtime + System.currentTimeMillis() - thetime) / seconds;
        return currenttime;

    } else {
        currenttime = (stoppedtime / seconds);
        return currenttime;
    }

   }
 }

输出:

1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.0 Seconds <-- Starting time to be zero
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <-- Start time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <-- Stop Time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 1.304 Seconds <-- Time of stopwatch
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <- Started time from 1.304 Seconds
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 1.788 Seconds <- After immediately checking time post start as you can see timer started from previous recorded time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <- Stop time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 4.988 Seconds <--Combined time for Lap 1 + Lap 2
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
4 <- Reset time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.0 Seconds <- Time reset to 0 Seconds
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
1 <- Start time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
3 <- Stop  time
1 start 2 is started 3 stop 4 reset 5 check time 6 stop
5
saved time is 0.796 Seconds <- Previous lap time not recorded as reset was done

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将当前时间保存为 Javascript 中的 cookie

来自分类Dev

将Hubot输入保存为变量

来自分类Dev

获取表中最高 4 个值的计数并保存为变量

来自分类Dev

使用pdo fetch_group将sql计数保存为单个变量

来自分类Dev

将名称保存为当前时间的文件

来自分类Dev

将href保存为var并使用该值包装另一个元素

来自分类Dev

使用gtk将当前窗口保存为图像

来自分类Dev

将闭包保存为变量理解

来自分类Dev

如何将boxplot保存为变量?

来自分类Dev

将系数和标准误差保存为变量

来自分类Dev

机架将图像变量保存为图像到磁盘

来自分类Dev

如何将CSV文件保存为变量

来自分类Dev

Python / Pandas:将数据框的频率保存为变量

来自分类Dev

将.py变量保存为序列的Pythonic方法

来自分类Dev

将更改的网址保存为javascript变量?

来自分类Dev

将系数和标准误差保存为变量

来自分类Dev

批处理 - For 循环 - 将每个结果保存为变量?

来自分类Dev

将 php 变量保存为 sql 的 null

来自分类Dev

哈希不是将日期保存为值吗?

来自分类Dev

Python将值保存为实例方法而不是整数

来自分类Dev

将选中的复选框值保存为数组

来自分类Dev

JPA @OneToMany 关联将 id 保存为空值

来自分类Dev

将提交的表单值保存为 cookie

来自分类Dev

无法将createdAt和updatedAt保存为日期时间值,也不能后端都不能前端

来自分类Dev

保存为变量时隐藏输出

来自分类Dev

fwrite()变量未保存为“ $ variable”

来自分类Dev

查找每列的唯一值计数并保存为CSV

来自分类Dev

检查当前页面上是否存在数组中的任何类,并使用 jQuery 将匹配的类保存为变量

来自分类Dev

如何将当前参考保存为JSON格式的父节点?

Related 相关文章

热门标签

归档