求助 关于一个C# 倒计时的求助

Gorgeouss 2009-02-24 09:54:01
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace timer
{
class Program
{
static int counter = 10;//倒计时

static void Main(string[] args)
{
Timer T = new Timer(1000);//1s
T.Elapsed += new ElapsedEventHandler(WriteChar);
T.Start();
Console.ReadLine();
}
public static void WriteChar(object source, ElapsedEventArgs e)
{
Console.WriteLine(counter--);
}


}
}

这是个控制台的倒计时程序到0之后就开始变成负数了 请问怎么修改?
然后 我想让这个倒计时数字停留在一个地方变化 而不是10 完了时候 9 就在下一行了
我是个初学者 对这个问题还不是很了解
请知道的人 帮忙 非常感谢
...全文
132 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljb07976513524 2009-05-29
  • 打赏
  • 举报
回复

DateTime topTime = DateTime.Now;
int secondTime = 12;
int row = Console.CursorTop;
int column = Console.CursorLeft;
Console.WriteLine(secondTime);
while (secondTime > 0)
{
TimeSpan xx = (DateTime.Now - topTime);
if (xx.TotalMilliseconds == 1000)
{
topTime = DateTime.Now;
secondTime--;
Console.SetCursorPosition(column, row);
if (secondTime < 10)
{
Console.WriteLine("0"+secondTime.ToString());
Console.WriteLine("" + secondTime.ToString());
Console.WriteLine(" " + secondTime.ToString());
}
else
Console.WriteLine(secondTime);
}
}
Console.ReadLine();
Wolf0403 2009-02-24
  • 打赏
  • 举报
回复
class Program
{
static int counter = 10;//倒计时
static Timer T = new Timer(1000);

static void Main(string[] args)
{
T.Elapsed += new ElapsedEventHandler(WriteChar);
T.Start();
Console.ReadLine();
}
public static void WriteChar(object source, ElapsedEventArgs e)
{
Console.WriteLine(counter--);
if (counter == 0) {
T.Stop();
}
}

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧