Java “如何停止倒计时”;计时器“;在爪哇?
我正在尝试为我正在创建的教育游戏制作一个倒计时计时器(只是为了学习),但我有一个小问题 总之,我只需要一个计时器:Java “如何停止倒计时”;计时器“;在爪哇?,java,timer,countdown,Java,Timer,Countdown,我正在尝试为我正在创建的教育游戏制作一个倒计时计时器(只是为了学习),但我有一个小问题 总之,我只需要一个计时器: 倒计时10秒 达到0秒时停止 当异常停止时抛出异常(我的“视图”将检测到该异常,以便向用户显示消息) 我的游戏有很多问题需要解决,每个问题都必须在10秒前解决。我创建了一个名为“Chronometer”的类来解决游戏中的问题,但我不知道如何停止它并抛出所需的异常(顺便说一下,这是联系我的视图的最佳方式?) 目前,它从10计数到0,但是停止,它继续,下一次它从59计数到0,它从不
- 倒计时10秒
- 达到0秒时停止
- 当异常停止时抛出异常(我的“视图”将检测到该异常,以便向用户显示消息)
请帮我解决这个问题 解决这个问题的一个好办法是使用观察者模式。有了这个模式,你就有了观察者和观察者。观察者可以观察可观察的事物(因此得名)。观察者不在乎是否有观察者在观察他 我提出的解决方案有两类,主要类(观察者)和计时类(可观察者)。主类将自己添加为计时器对象的观察者,当可观察者有东西要通知时,将运行方法
void update(Observable,object)
。计时器不再使用计时器了。相反,它使用的线程将休眠10秒,之后它将状态设置为“已修改”,并通知所有观察者,从而调用void更新(Observable,Object)
通过这种实现,天文钟将在完成时通知每个观察者。这将允许您知道何时必须更新视图
编辑:我已经更改了运行
和更新
方法的实现,因此观察员每秒都会收到通知
主要类别:
public class Main implements Observer{
public static void main(String[] args) {
Main m = new Main();
Chronometer c = new Chronometer(2014, 7, 4, 13, 46, 21, (byte) 0);
c.addObserver(m);
c.run();
try {
Thread.sleep(200000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void update(Observable arg0, Object arg1) {
int time = (int)arg1;
if(time > 0){
System.out.println("Time left: " + time);
} else {
System.out.println("The time's over!");
//Update View
}
}
}
天文钟:
public class Chronometer extends Observable implements Runnable{
private DateFormat format = new SimpleDateFormat("ss");
private Calendar calendar = Calendar.getInstance();
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public int getTime() {
calendar.add(Calendar.SECOND, countType);
return Integer.parseInt(format.format(calendar.getTime()));
}
@Override
public void run() {
int time = 10;
do {
try {
Thread.sleep(1000);
setChanged();
notifyObservers(time);
time--;
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (time >= 0);
}
}
尝试以下方法停止:
public class Chronometer {
private Calendar calendar = Calendar.getInstance();
private static final long TIME_0 = -62167489199561L;
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public void startChronometer(){
Timer t = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
if(getTime()==0) throw new RuntimeException("Done");
calendar.add(Calendar.SECOND, countType);
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
//Returns seconds left
public long getTime() {
return Math.round((calendar.getTime().getTime()-TIME_0)/1000);
}
public static void main(String[] args) {
Chronometer c = new Chronometer(0, 0, 0, 0, 0, 10, REGRESSIVE);
c.startChronometer();
}
}
对于字符串比较,您肯定应该使用
.equals
而不是=
,但是如果存在其他问题,最好是使示例可编译。哦,对不起,在我使用的上一个版本的代码中。equalsIgnoreCase,很抱歉输入错误。为什么要使用字符串来进行比较?使用整数会更有意义。对于以后的帖子,你最好等到答案真正完成后再提交,这也永远不会结束:PIt工作正常。。。请查收。这是Eclipse:9 8 7 6 5 4 3 2 1 0线程“Timer-0”java.lang.RuntimeException:在com.test.so.Chronometer$1.run(Chronometer.java:29)在java.util.TimerThread.mainLoop(未知源代码)在java.util.TimerThread.run(未知源代码)中的输出
public class Main implements Observer{
public static void main(String[] args) {
Main m = new Main();
Chronometer c = new Chronometer(2014, 7, 4, 13, 46, 21, (byte) 0);
c.addObserver(m);
c.run();
try {
Thread.sleep(200000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void update(Observable arg0, Object arg1) {
int time = (int)arg1;
if(time > 0){
System.out.println("Time left: " + time);
} else {
System.out.println("The time's over!");
//Update View
}
}
}
public class Chronometer extends Observable implements Runnable{
private DateFormat format = new SimpleDateFormat("ss");
private Calendar calendar = Calendar.getInstance();
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public int getTime() {
calendar.add(Calendar.SECOND, countType);
return Integer.parseInt(format.format(calendar.getTime()));
}
@Override
public void run() {
int time = 10;
do {
try {
Thread.sleep(1000);
setChanged();
notifyObservers(time);
time--;
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (time >= 0);
}
}
public class Chronometer {
private Calendar calendar = Calendar.getInstance();
private static final long TIME_0 = -62167489199561L;
private final byte countType;
public static final byte PROGRESSIVE = 1;
public static final byte REGRESSIVE = -1;
public Chronometer(int years, int months, int days, int hours, int minutes, int seconds, byte countType) {
calendar.set(years, months, days, hours, minutes, seconds);
this.countType = countType;
}
public void startChronometer(){
Timer t = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println(getTime());
if(getTime()==0) throw new RuntimeException("Done");
calendar.add(Calendar.SECOND, countType);
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
//Returns seconds left
public long getTime() {
return Math.round((calendar.getTime().getTime()-TIME_0)/1000);
}
public static void main(String[] args) {
Chronometer c = new Chronometer(0, 0, 0, 0, 0, 10, REGRESSIVE);
c.startChronometer();
}
}