<pre name="code" class="java">package com.lovo;
import java.util.Calendar;
/**
* 时钟类
* @author 李睿
*
*/
public class Clock {
private int hour;
private int min;
private int second;
public Clock(int hour, int min, int second) {
this.hour = hour;
this.min = min;
this.second = second;
}
public Clock(){
Calendar cal = Calendar.getInstance();
hour = cal.get(11);
min = cal.get(12);
second = cal.get(13);
}
/**
* 时钟运行
*/
public void run(){
second ++;
if (second == 60) {
min += 1;
second =0;
if (min == 60) {
hour += 1;
min = 0;
}
}
if (hour == 24 && min == 0 && second == 0){
hour = 0;
min = 0;
second = 0;
}
}
/**
* 显示时间
* @return
*/
public String showTime(){
String time = "";
if (hour < 10){
time += "0";
}
time += hour + ":";
if (min < 10){
time += "0";
}
time += min + ":";
if (second < 10){
time += "0";
}
time += second;
return time;
}
}
<pre name="code" class="java">package com.lovo;
/**
* 时钟运行显示时间
* @author 李睿
*
*/
public class Text03 {
public static void main(String[] args) throws Exception {
Clock c1 = new Clock();
for (; ;) {
c1.run();
System.out.println(c1.showTime());
//停顿1s
Thread.sleep(1000);
}
}
}
时钟类
最新推荐文章于 2023-04-05 19:18:12 发布