C# 控制台 模拟时间一秒一秒走动,直到按Esc键,时间静止,退出!
生活随笔
收集整理的這篇文章主要介紹了
C# 控制台 模拟时间一秒一秒走动,直到按Esc键,时间静止,退出!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Code
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading;?//延遲函數必備
namespace?ConsoleApplication2
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????DateTime?dt?=?new?DateTime();
????????????bool?ToBeContinue=true;
????????????while?(ToBeContinue)
????????????{
????????????????ConsoleKeyInfo?keyInfo;
????????????????if?(System.Console.KeyAvailable)
????????????????{
????????????????????keyInfo?=?System.Console.ReadKey(true);
????????????????????//if?(keyInfo.Key.ToString()=="Enter")//如果按了Enter鍵
????????????????????if?(keyInfo.KeyChar?==?(char)027)//如果按了Esc鍵,Esc鍵的ASCII碼為十進制27
????????????????????????ToBeContinue?=?false;
????????????????}
????????????????else//沒有按Esc鍵,時間一秒一秒走動
????????????????{
????????????????????dt?=?DateTime.Now;//獲取當前時間
????????????????????Thread.Sleep(1000);//延遲1秒
????????????????????Console.Clear();
????????????????????Console.WriteLine(dt);
????????????????}
????????????}
????????????Console.WriteLine("你已經按了Esc鍵退出,時間靜止!");
????????????Console.ReadKey();
????????}
????}
}
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading;?//延遲函數必備
namespace?ConsoleApplication2
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????DateTime?dt?=?new?DateTime();
????????????bool?ToBeContinue=true;
????????????while?(ToBeContinue)
????????????{
????????????????ConsoleKeyInfo?keyInfo;
????????????????if?(System.Console.KeyAvailable)
????????????????{
????????????????????keyInfo?=?System.Console.ReadKey(true);
????????????????????//if?(keyInfo.Key.ToString()=="Enter")//如果按了Enter鍵
????????????????????if?(keyInfo.KeyChar?==?(char)027)//如果按了Esc鍵,Esc鍵的ASCII碼為十進制27
????????????????????????ToBeContinue?=?false;
????????????????}
????????????????else//沒有按Esc鍵,時間一秒一秒走動
????????????????{
????????????????????dt?=?DateTime.Now;//獲取當前時間
????????????????????Thread.Sleep(1000);//延遲1秒
????????????????????Console.Clear();
????????????????????Console.WriteLine(dt);
????????????????}
????????????}
????????????Console.WriteLine("你已經按了Esc鍵退出,時間靜止!");
????????????Console.ReadKey();
????????}
????}
}
轉載于:https://www.cnblogs.com/zhangzufa/archive/2009/04/29/1446597.html
總結
以上是生活随笔為你收集整理的C# 控制台 模拟时间一秒一秒走动,直到按Esc键,时间静止,退出!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序阻止冒泡点击_微信小程序bin
- 下一篇: Win10桌面背景(壁纸)导出工具