第1个回答 2013-01-13
public class Clock {
int hour=0;
int minute=0;
int second=0;
public Clock(){
hour=0;
minute=0;
second=0;
}
public void setClockTime(int hour,int minute,int second) throws SetTimeException {
if(hour<0||hour>23){
throw new SetTimeException("输入小时有误:"+hour);
}
if(minute<0||minute>59){
throw new SetTimeException("输入分钟有误:"+minute);
}
if(second<0||second>59){
throw new SetTimeException("输入秒有误:"+second);
}
this.hour=hour;
this.minute=minute;
this.second=second;
}
public void incSecond(){
if(second>=59){
second=0;
if(minute>=59){
minute=0;
if(hour>=23){
hour=0;
}else{
hour++;
}
}else{
minute++;
}
}else{
second++;
}
}
public String getTime(){
String time=hour+":"+minute+":"+second;
return time;
}
public static void main(String[] args) throws SetTimeException {
Clock c=new Clock();
System.out.println(c.getTime());
c.setClockTime(21, 59, 58);
System.out.println(c.getTime());
for(int i=0;i<10;i++){
c.incSecond();
System.out.println(c.getTime());
}
}
}
class SetTimeException extends Exception{
public SetTimeException(String msg){
super(msg);
}
}