java Timer倒数计时器(急)
求一个用Timer写的倒数计时器,不要用线程休眠来实现的!要实现的就是倒数5秒,5,4,3,2,1,0当0的时候就停止计时。求JAVA代码。好的加分...
求一个用Timer写的倒数计时器,不要用线程休眠来实现的!要实现的就是倒数5秒,5,4,3,2,1,0当0的时候就停止计时。求JAVA 代码。好的加分
展开
4个回答
展开全部
用那么复杂吗?
二行代码行搞定了
楼主知道不知道,,sleep方法,,,
程序暂停用的,
public class Demo_01 {
static int i = 1 ;
public static void main(String[] args) throws Exception {
while(true){
Thread.sleep(1000);
System.out.println(5-i);
i++;
if(i==5)System.out.println("你去死吧");
if(i==5)break;
}
}
}
二行代码行搞定了
楼主知道不知道,,sleep方法,,,
程序暂停用的,
public class Demo_01 {
static int i = 1 ;
public static void main(String[] args) throws Exception {
while(true){
Thread.sleep(1000);
System.out.println(5-i);
i++;
if(i==5)System.out.println("你去死吧");
if(i==5)break;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.*;
class MyTimer extends TimerTask
{
private Timer timer;
private int time;
MyTimer()
{
}
MyTimer(Timer timer,int time)
{
this.time=time;
this.timer=timer;
}
public void run()
{
System.out.println(this.time);
this.time--;
if(this.time<0)
{
this.timer.cancel();
}
}
}
public class eee{
public static void main(String args[])
{
Timer t = new Timer();
int s=5;
MyTimer mt=new MyTimer(t,s);
t.scheduleAtFixedRate(mt,0,1000);
}
}
class MyTimer extends TimerTask
{
private Timer timer;
private int time;
MyTimer()
{
}
MyTimer(Timer timer,int time)
{
this.time=time;
this.timer=timer;
}
public void run()
{
System.out.println(this.time);
this.time--;
if(this.time<0)
{
this.timer.cancel();
}
}
}
public class eee{
public static void main(String args[])
{
Timer t = new Timer();
int s=5;
MyTimer mt=new MyTimer(t,s);
t.scheduleAtFixedRate(mt,0,1000);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给个参考
public class time {
private boolean onetime = true;
public static void main(String[] args) {
time t = new time();
}
time() {
new ChangeLabel("00:5").start();
}
class ChangeLabel extends Thread // ?s•b??’ö
{
private int minitues;
private String Sminitues;
private int sound;
private String Ssound;
private String LabelTime;
public ChangeLabel(String time) {
// TODO Auto-generated constructor stub
onetime = false;
this.minitues = Integer.parseInt(time.substring(0, time
.indexOf(':')));
this.sound = Integer
.parseInt(time.substring(time.indexOf(':') + 1));
}
private long time1;
private long time2;
public void run() {
time1 = System.currentTimeMillis();
while (true) {
time2 = System.currentTimeMillis();
while (!(minitues == 0 & sound == 0) && time2 >= time1 + 1000) {
time1 = time2;
if (sound == 0) {
sound = 59;
minitues--;
} else {
sound--;
}
}
if (minitues == 0 && sound == 0) {
onetime = true;
System.out.println("ok");
break;
}
}
}
}
}
public class time {
private boolean onetime = true;
public static void main(String[] args) {
time t = new time();
}
time() {
new ChangeLabel("00:5").start();
}
class ChangeLabel extends Thread // ?s•b??’ö
{
private int minitues;
private String Sminitues;
private int sound;
private String Ssound;
private String LabelTime;
public ChangeLabel(String time) {
// TODO Auto-generated constructor stub
onetime = false;
this.minitues = Integer.parseInt(time.substring(0, time
.indexOf(':')));
this.sound = Integer
.parseInt(time.substring(time.indexOf(':') + 1));
}
private long time1;
private long time2;
public void run() {
time1 = System.currentTimeMillis();
while (true) {
time2 = System.currentTimeMillis();
while (!(minitues == 0 & sound == 0) && time2 >= time1 + 1000) {
time1 = time2;
if (sound == 0) {
sound = 59;
minitues--;
} else {
sound--;
}
}
if (minitues == 0 && sound == 0) {
onetime = true;
System.out.println("ok");
break;
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询