目录
1 问题描述
实现一个时间类Time。将小时,分钟和秒存储为int型成员变量。要求该类中包含一个构造函数,访问用的函数,一个推进当前时间的函数adv(),一个重新设置当前时间(即将当前时间设为00:00:00)的函数reset()和输出结果函数print()。注意时间按数字式电子表格式显示,即小时、分、秒分别用两位表示,如果其中之一小于10,则前方补0,如22:01:00(中间不含空格),另外按该格式依次输出时、分、秒后,以endl结尾。当输入时间超出合法范围(提示:注意上下界)时,请自动按照24小时制进行转换,请务必注意时分秒为负值时的处理,如输入25:00:61,则输出应为01:01:01,输入-1:-1:-1,应该输出22:58:59。
第一步:依据题意定义Time类
class Time
{
// 请补充
};
第二步:利用如下测试程序对所编写的Time类进行测试。
int main()
{
// 当前时间
int hour, minute, second;
// 时间增量
int incr_hr, incr_min, incr_sec;
cin >> hour >> minute >> second >> incr_hr >> incr_min >> incr_sec;
Time t( hour, minute, second );
t.print();
t.adv( incr_hr, incr_min, incr_sec );
t.print();
t.reset();
t.print();
return 0;
}
第一行为初始时间,第二行为adv()后的时间,第三行为reset()后的时间
23:59:59
00:00:00
示例3:
02:04:02
00:00:00
00:22:30
00:00:00
2 解决方案
本题主要考查类的相关定义及应用,下面具体代码对于时、分、秒的数据范围处理的部分代码,可以单独写一个函数来处理,这样可以复用代码,但是为了配合题目要求,就直接把代码复制了一遍运用。
具体代码如下:
import java.util.Scanner; public class Main { public int hour; public int minute; public int second; Main(int hour, int minute, int second) { //获取秒 if(second >= 60) { minute += second / 60; second %= 60; this.second = second; } else if(second < 0) { int temp = second / 60; int temp1 = second % 60; if(temp1 == 0) { this.second = 0; minute += temp; } else { this.second = 60 + temp1; minute += temp - 1; } } else { this.second = second; } //获取分钟 if(minute >= 60) { hour += minute / 60; minute %= 60; this.minute = minute; } else if(minute < 0) { int temp = minute / 60; int temp1 = minute % 60; if(temp1 == 0) { this.minute = 0; hour += temp; } else { this.minute = 60 + temp1; hour += temp - 1; } } else { this.minute = minute; } //获取小时 if(hour >= 24) this.hour = hour % 24; else if(hour < 0) this.hour = 24 + hour % 24; else this.hour = hour; } public void adv(int hour, int minute, int second) { second += this.second; //获取要求格式秒 if(second >= 60) { minute += second / 60; second %= 60; this.second = second; } else if(second < 0) { int temp = second / 60; int temp1 = second % 60; if(temp1 == 0) { this.second = 0; minute += temp; } else { this.second = 60 + temp1; minute += temp - 1; } } else { this.second = second; } minute += this.minute; //获取要求格式分钟 if(minute >= 60) { hour += minute / 60; minute %= 60; this.minute = minute; } else if(minute < 0) { int temp = minute / 60; int temp1 = minute % 60; if(temp1 == 0) { this.minute = 0; hour += temp; } else { this.minute = 60 + temp1; hour = hour + temp - 1; } } else { this.minute = minute; } hour += this.hour; //获取要求格式小时 if(hour >= 24) this.hour = hour % 24; else if(hour < 0) this.hour = 24 + hour % 24; else this.hour = hour; } public void reset() { this.hour = 0; this.minute = 0; this.second = 0; } public void print() { if(hour >= 10) System.out.print(hour+":"); else { System.out.print("0"+hour+":"); } if(minute >= 10) System.out.print(minute+":"); else System.out.print("0"+minute+":"); if(second >= 10) System.out.print(second+"\n"); else System.out.print("0"+second+"\n"); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int hour1 = in.nextInt(); int minute1 = in.nextInt(); int second1 = in.nextInt(); int hour2 = in.nextInt(); int minute2 = in.nextInt(); int second2 = in.nextInt(); Main test = new Main(hour1, minute1, second1); test.print(); test.adv(hour2, minute2, second2); test.print(); test.reset(); test.print(); } }