一、实验内容
- 题目1
对程序的测试结果:
三、程序源代码
1.Main类
public class Main {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt(),in.nextInt(), in.nextInt(), in.nextInt());//起始时间
// for (;;){
clock.tick();
System.out.println(clock.toString());
// }
// in.close();
}
}
2.Clock类
public class Clock {
private Display hour = new Display(24);
private Display minute = new Display(60);
private Display second = new Display(60);
private Display year = new Display(10000);
private Display month = new Display(12);
private Display day = new Display(32);
public Clock() {
}
//有参构造函数
public Clock(int hour, int minute, int second, int year, int month, int day){
this.hour.setValue(hour);
this.minute.setValue(minute);
this.second.setValue(second);
//月和日最低不是0,所以输出的时候要加一,操作时和实际的相比较则要减一
this.year.setValue(year);
this.month.setValue(month);
this.day.setValue(day);
}
public void start()
{
for(;;)
{
minute.increase();
if (minute.getValue() == 0)
{
hour.increase();
}
}
}
public void tick() {
second.increase();
if (second.getValue() == 0) {
minute.increase();
if (minute.getValue() == 0) {
hour.increase();
if (hour.getValue() == 0) {
day.increase();
if(day.getValue() == 0)
month.increase();
else if ((day.getValue() == 28 || day.getValue() == 27) && month.getValue()==2) {
month.increase();
day.setValue(0);
}
if(month.getValue() == 0){
year.increase();
}
}
}
}
}
@Override
public String toString() {
return String.format("%02d/%02d/%02d,%02d:%02d:%02d",
year.getValue(),month.getValue(),day.getValue(),hour.getValue(), minute.getValue(), second.getValue());
}
public static void main(String[] args) {
Clock c = new Clock();
c.start();
}
}
3.Display类
public class Display {
private int value=0;
private int limit=0;
public Display(int limit)
{
this.limit = limit;
}
public void increase()
{
value++;
if (value == limit)
{
value = 0;
}
}
public int getValue()
{
return value;
}
//设置当前值
public void setValue(int value){
this.value = value;
}
public static void main(String[] args) {
Display d = new Display(60);
// d.main(args);
for(;;)
{
d.increase();
System.out.println(d.limit);
}
}
}