小问题,要线等.
package com;
import java.util.*;
public class Alert {
Timer timer;
public Alert(){
timer=new Timer();
timer.schedule(new RemindTask(),0,1*1000);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Alert();
}
}
package com;
import java.util.TimerTask;
public class RemindTask1 extends TimerTask {
int numWarning=5;
public void run() {
if(numWarning==5){
System.out.println( "Alert! ");
numWarning--;
}
else{
System.out.println( "Time 's up! ");//怎么是执行这行的?
System.exit(0);
}
}
}
运行结果:
Time 's up!
怎么结果不是这个:
Alert!
Alert!
Alert!
Alert!
Alert!
高手请教,谢谢!
------解决方案--------------------import java.util.*;
public class Alert {
Timer timer;
public Alert(){
timer=new Timer();
timer.schedule(new RemindTask(),0,2*1000);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Alert();
}
}
class RemindTask extends TimerTask {
int numWarning=5;
public void run() {
if(numWarning > 0){
System.out.println( "Alert! ");
numWarning--;
}
else{
System.out.println( "Time 's up! ");//怎么是执行这行的?
System.exit(0);
}
}
}