如何在java中制作倒数计时器 [英] how to make a countdown timer in java
问题描述
我是编程的初学者(学生)并被指派创建游戏。
我正在制作的游戏叫做boggle。
玩家必须在给定时间内在随机信件板中找到单词。
但是我在创建计时器时遇到了麻烦。
这是我的计时器应该做的:
I'm a beginner(Student) in programming and was assigned to create a game. The game I'm making is called boggle. In which the player have to find words in a random letter board within a given time. but I'm having trouble with creating the timer. This is what it my timer should do:
- 动态输入时间(设定时间)
- 从输入时间倒计时到0
- 当o =>跳出循环时
我需要知道的是如何使其倒计时。
我认为我不需要ActionListener,因为它在创建类的那一刻开始滴答作响。
All i need to know is how to make it countdown. I don't think i need a ActionListener because it starts ticking the moment the class is created.
任何帮助,建议,链接,正确方向的推动都将张开双臂接受。
Any help, advice, links, push in the right direction will be accepted with open arms.
推荐答案
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Stopwatch {
static int interval;
static Timer timer;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input seconds => : ");
String secs = sc.nextLine();
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = Integer.parseInt(secs);
System.out.println(secs);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(setInterval());
}
}, delay, period);
}
private static final int setInterval() {
if (interval == 1)
timer.cancel();
return --interval;
}
}
试试这个。
这篇关于如何在java中制作倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!