c#设计一个时间类(time),该类能显示时间,并对时,分,秒做加一和减一的操作
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询