简单模拟一个闹钟。要求:每隔一秒钟就让该闹钟加一秒。每个整点钟都报一次时。
初学者。。。。
------解决方案--------------------
package com.text;
import java.util.Date;
public class NaoZhong {
public static void main(String[] args){
Date date = new Date();
int second = date.getSeconds();
int minute = date.getMinutes();
int hour = date.getHours();
do{
second++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(second==60){
second=0;
if(minute<59){
minute++;
}else{
minute=0;
}
if(minute==0){
if(hour<23){
hour++;
System.out.println("闹铃响");
}else{
hour=0;
System.out.println("闹铃响");
}
}
}
System.out.println(hour+":"+minute+":"+second);
}while(1==1);
}
}
//你把这段代码在eclipse 或 myeclipse上运行一下看看