C# 控制台程序,定义一个时钟类,包含 :时,分,秒, 3个字段,要求能够设置时间,显示时间,时间能够走
1个回答
展开全部
using System;
using System.Timers;
namespace ConsoleApp
{
public class Clock
{
private Timer timer;
private int hour;
private int minute;
private int second;
public Clock()
{
timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
public void Start()
{
timer.Start();
}
public void Stop()
{
timer.Stop();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime time = e.SignalTime;
hour = time.Hour;
minute = time.Minute;
second = time.Second;
string display = string.Format("{0}:{1}:{2}",
hour, minute, second);
Console.Clear();
Console.WriteLine(display);
}
}
}
Main函数里写new Clock().Start();
即可。
using System.Timers;
namespace ConsoleApp
{
public class Clock
{
private Timer timer;
private int hour;
private int minute;
private int second;
public Clock()
{
timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
public void Start()
{
timer.Start();
}
public void Stop()
{
timer.Stop();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime time = e.SignalTime;
hour = time.Hour;
minute = time.Minute;
second = time.Second;
string display = string.Format("{0}:{1}:{2}",
hour, minute, second);
Console.Clear();
Console.WriteLine(display);
}
}
}
Main函数里写new Clock().Start();
即可。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询