欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Timer定时器

程序员文章站 2022-07-14 13:17:39
...
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

class Time{
	int time;
	int minute;
	int second;
	public void setTime(int x,int y,int r){
		time = x;
		minute = y;
		second = r;
	}
	public void display(){
		System.out.printf("%2d:%2d:%2d\n",time,minute,second);
	}
	public void tick(){
		second++; 
		if(second >= 60){
			second = 0;
			minute++;
		}
		if(minute >= 60){
			minute=0;
			time++;
		}
		if(time==24)
			time = 0;
	}
	
}
public class Main {
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int x = input.nextInt(); 
		int y = input.nextInt(); 
		int r = input.nextInt();
	
		Time va = new Time();
		va.setTime(x,y,r);
		
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				va.tick();
				va.display();
				
			}
		}, 1000, 1000);
		
	}
}