Visual studio 如何在控制台应用程序中创建持续更新的时钟?
com我正在为我的编程考试做一个项目,这将是一个简单的考试,因此我只需要创建一个简单、基本的控制台应用程序。但是,即使它很简单,我真的想给它添点味道。 我已经制作了一个简单的时钟:Visual studio 如何在控制台应用程序中创建持续更新的时钟?,visual-studio,console,clock,Visual Studio,Console,Clock,com我正在为我的编程考试做一个项目,这将是一个简单的考试,因此我只需要创建一个简单、基本的控制台应用程序。但是,即使它很简单,我真的想给它添点味道。 我已经制作了一个简单的时钟: static public void clock() { Console.SetCursorPosition(0, 0); Console.WriteLine("{0:D} {0:t}", DateTime.Now); Console.Write
static public void clock()
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("{0:D} {0:t}", DateTime.Now);
Console.WriteLine("");
}
我在程序中使用名称“时钟”引用此方法,如下所示:
Console.Clear();
clock();
Console.WriteLine("┌───────────────────────────────────┐");
Console.WriteLine("| Welcome to the Festival |");
Console.WriteLine("└───────────────────────────────────┘");
有没有可能在时钟上加上秒数,使其不断更新,并以一种简单的方式进行?一种新手程序员可以解释的方式,我需要这样做。
谢谢大家! 这绝对不是万无一失的,因为没有“简单”的方法来正确地做到这一点……但它可能符合您的目的:
static void Main(string[] args)
{
Task.Run(() => {
while (true)
{
// save the current cursor position
int x = Console.CursorLeft;
int y = Console.CursorTop;
// update the date/time
Console.SetCursorPosition(0, 0);
Console.Write(DateTime.Now.ToString("dddd, MMMM d, yyyy hh:mm:ss"));
// put the cursor back where it was
Console.SetCursorPosition(x, y);
// what one second before updating the clock again
System.Threading.Thread.Sleep(1000);
}
});
Console.SetCursorPosition(0, 2);
Console.WriteLine("┌───────────────────────────────────┐");
Console.WriteLine("| Welcome to the Festival |");
Console.WriteLine("└───────────────────────────────────┘");
Console.WriteLine("");
Console.Write("Please enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello {0}!", name);
Console.WriteLine("");
Console.Write("Press Enter to Quit...");
Console.ReadKey();
}
如果您将代码作为文本直接包含在问题中,而不是要求人们访问外部站点,您的问题将得到改进,并将被更多人考虑。谢谢您,哈切特,我已经用代码片段而不是图像重新回答了这个问题。