如何在 java(秒表 Java)中制作倒数计时器

How to make a countdown CountDown Timer in java ( stopwatch Java )

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 时停止?

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

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

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

您可能需要更改

Timer time = new Timer();

final Timer time = new Timer();

(取决于您的 Java 版本)告诉编译器可以保存从匿名内部 class.

内部访问外部局部变量
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;
    }

}

这是针对您的问题的快速解决方案。您必须按如下方式调用方法 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;
    }

} 

多米尼克·乌伯斯菲尔德