回覆列表
  • 1 # 錢布斯

    程式碼如下:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace Demo5

    {

    class Time

    {

    private int hour;

    private int minute;

    private int second;

    public Time(int hour, int minute, int second)

    {

    this.hour = hour;

    this.minute = minute;

    this.second = second;

    }

    public void IncrementHour()

    {

    if (this.hour < 23)

    {

    this.hour++;

    }

    else

    {

    this.hour = 0;

    }

    }

    public void DecrementHour()

    {

    if (this.hour > 0)

    {

    this.hour--;

    }

    else

    {

    this.hour = 23;

    }

    }

    public void IncrementMinute()

    {

    if (this.minute < 59)

    {

    this.minute++;

    }

    else

    {

    this.minute = 0;

    IncrementHour();

    }

    }

    public void DecrementMinute()

    {

    if (this.minute > 0)

    {

    this.minute--;

    }

    else

    {

    this.minute = 59;

    DecrementHour();

    }

    }

    public void IncrementSecond()

    {

    if (this.second < 59)

    {

    this.second++;

    }

    else

    {

    this.second = 0;

    IncrementMinute();

    }

    }

    public void DecrementSecond()

    {

    if (this.second > 0)

    {

    this.second--;

    }

    else

    {

    this.second = 59;

    DecrementMinute();

    }

    }

    public void Display()

    {

    Console.WriteLine("{0}:{1}:{2}", hour, minute, second);

    }

    }

    class Program

    {

    static void Main(string[] args)

    {

    Time time = new Time(22, 33, 20);

    time.Display();

    // 小時加1

    time.IncrementHour();

    time.Display();

    // 小時減1

    time.DecrementHour();

    time.Display();

    // 分鐘加1

    time.IncrementMinute();

    time.Display();

    // 分鐘減1

    time.DecrementMinute();

    time.Display();

    // 秒加1

    time.IncrementSecond();

    time.Display();

    // 秒減1

    time.DecrementSecond();

    time.Display();

    Console.ReadKey();

    }

    }

    }

  • 中秋節和大豐收的關聯?
  • 酒精中毒昏迷怎麼辦?