【问题标题】:How to make a countdown CountDown Timer in java ( stopwatch Java )如何在 Java 中制作倒数倒计时计时器(秒表 Java)
【发布时间】:2017-02-28 13:09:32
【问题描述】:
package test;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class StopWatch 
{
    public static int interval;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        interval = input.nextInt();
        int delay = 1000;
        int period = 1000;
        Timer time = new Timer();
        System.out.println(interval);
        time.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                if (interval == 0) {
                    System.out.println("work finished");
                } else {
                    System.out.println(setInterval());
                }
            }
        }, delay, period);
    }

    private static int setInterval() {          
        return --interval;
    }    
}

我有一个问题,当我运行时它不会停止计数!

那么,当计数器达到 0 时,我该如何让它停止呢?

【问题讨论】:

    标签: java countdowntimer stopwatch


    【解决方案1】:

    您需要指示计时器在最后一次迭代时停止;喜欢:

    public void run() {
       if (interval == 0) {
         System.out.println("work finished");
         time.cancel();
         time.purge();
    

    有关详细信息,请参阅javadoc。您可能不会在这里调用purge(),因为只有一个线程处理该计时器;但在更“通用”的用例中,它是必需的。

    你可能需要改变

    Timer time = new Timer();
    

    final Timer time = new Timer();
    

    (取决于您的 Java 版本)告诉编译器可以从匿名内部类中访问外部局部变量。

    【讨论】:

      【解决方案2】:
      package com.test;
      
      import java.util.Scanner;
      import java.util.Timer;
      import java.util.TimerTask;
      
      public class StopWatch 
      {
          public static int interval;
      
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              System.out.print("Input seconds => : ");
              interval = input.nextInt();
              int delay = 1000;
              int period = 1000;
              final Timer time = new Timer();
              System.out.println(interval);
              time.scheduleAtFixedRate(new TimerTask() {
      
                  public void run() {
                      if (interval == 0) {
                          System.out.println("work finished");
                          time.cancel();
                          time.purge();
                      } else {
                          System.out.println(setInterval());
                      }
                  }
              }, delay, period);
          }
      
          private static int setInterval() {
      
              return --interval;
          }
      
      }
      

      【讨论】:

      • 提示:答案只是向读者扔代码......不是很有帮助。既不是对你(很少赞成)也不是对读者;因为有个解释。你知道,拿我写的三行来说;并将它们放入原始源代码中……并不能完全弥补这一点。
      【解决方案3】:

      这里是您的问题的快速解决方案。您必须调用方法 cancel() 如下:

      public static int interval;
      
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              System.out.print("Input seconds => : ");
              interval = input.nextInt();
              int delay = 1000;
              int period = 1000;
              Timer time = new Timer();
              System.out.println(interval);
              time.scheduleAtFixedRate(new TimerTask() {
      
                  public void run() {
                      if (interval == 0) {
                          System.out.println("work finished");
                          time.cancel();
                      } else {
                          System.out.println(setInterval());
                      }
                  }
              }, delay, period);
          }
      
          private static int setInterval() {
      
              return --interval;
          }
      
      } 
      

      多米尼克·尤伯斯菲尔德

      【讨论】:

      • 提示:采用已经存在的两个答案,将它们放在一起......不提供任何进一步的见解或“附加价值”......不会为您带来支持。
      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多