java实验报告_时钟(浙江大学慕课瓮恺)

一、实验内容

  1. 题目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();
    }
/*18 12 30 2020 8 31 */
}

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();
            }

        }
    }

    /*输出String的值,以“yyyy:mm:dd:hh:mm:ss“的形式表示当前时间。
    这里除了年份占四位外,每个数值都占据两位,不足两位时补0。如“00:01:22"。注意其中的冒号是西文的,
    不是中文的。(String.format()可以用和printf一样的方式来格式化一个字符串。)
*/
    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);
        }

    }

}

猜你喜欢

转载自blog.csdn.net/qq_45768060/article/details/108027536
今日推荐