1

我想创建一个这样的程序。
每一分钟的时间都应该以 h:m.
每 5 分钟它应该打印“break”
这应该持续 24 小时 .. 像这样

0:0
0:1
0:2
0:3
0:4
break
0:6
0:7
0:8
0:9
break
0 :11

.
.
23:59

我带来了一个解决它的程序..但我从未使用过DateTime或任何时间功能,我只是习惯于Thread.Sleep每次打印 1 分钟......我想使用其他方法而不是Thread.Sleep解决它......所以请指导我..(对不起我的英语不好)


我就是这样做的Thread.Sleep
请为我提供任何其他解决方案

using System;
using System.Threading;

class try22
{
    public static void Main()
    {
        for(int i=0;i<24;i++)
        {
            for(int j=0;j<60;j++)
            { 
                if(j%5 != 0 || (j == 0 && i == 0))
                {
                    Thread.Sleep(20);        
                    Console.WriteLine(i+":"+j);
                }
                else if(j%5 == 0 )
                {
                    Thread.Sleep(20);
                    Console.WriteLine("break");
                }
            }
        }
     }
}




谢谢大家,我想出了在我的问题中使用实际日期而不是数组数字的解决方案,我

的计时器出现了奇怪的错误.. :( 所以我使用了 thread.sleep itslef

using System;
using System.Threading;

class try22
{
    public static void Main()
    { 
        DateTime dt1 = new DateTime();
        dt1 = DateTime.ParseExact("0:0", "H:m",null);
        int cford=dt1.Day+1;
        for (; dt1.Day!=cford; )
        {
           dt1 = addm(dt1);
           Console.WriteLine(dts(dt1));
           Thread.Sleep(60000);
        }
    }
    public static string dts(DateTime dt)
    {

        string tmp = dt.ToString("H:m");
        if (dt.Minute % 5 == 0)
            return "BREAK";
        else
            return tmp;
    }
    public static DateTime addm(DateTime dt)
    {
        return dt.AddMinutes(1);
    }
}
4

3 回答 3

1

你问的是哪一个?

  1. 每分钟显示一次当前时间
  2. 在每分钟开始时显示当前时间,就像闹钟一样

假设 1,这里有一些正确方向的提示(无论哪种方式都应该有帮助):

  1. 您可以使用获取当前日期和时间作为DateTime对象DateTime.Now
  2. DateTime对象可以使用 . 返回自定义字符串输出.ToString("format")
  3. 格式使用自定义日期和时间格式字符串指定。例如,要获取 24 小时制的当前小时(不带前导零),您可以使用DateTime.Now.ToString("H").
  4. 根据参考,您可以在格式中包含字符串文字(未处理的字符串)。例如DateTime.Now.ToString("'Hour is: 'H")会返回Hour is: 6
  5. 您可以将DateTime对象的“分钟”值作为intusing获取.Minute。例如,int minute = DateTime.Now.Minute;
  6. 如果您希望某些代码定期运行,一种方法是将其移动到自己的方法中,然后System.Threading.Timer像这样设置:

    void SomeMethod(object state) { /* DO STUFF HERE */ }
    
    // Initialise the timer in your main() method 
    // As per MSDN for System.Threading.Timer, first number (0) is start delay.
    // Second number (60000) is interval in milliseconds (60 seconds)
    // This will cause SomeMethod to be called once every 60 seconds starting now.
    Timer timer = new Timer(new TimerCallback(SomeMethod), null, 0, 60000);
    
  7. 制作完成后,您需要立即停止应用程序退出Timer(否则它将永远无法运行)。在命令行应用程序中执行此操作的一种简单方法是将 aConsole.Read()放在方法的末尾,该Main()方法将等待用户输入。

于 2012-07-06T03:58:01.830 回答
1

我使用了 Timer 而不是 Thread

 class Program
    {
        private static System.Timers.Timer aTimer;
        static int j = 0;
        static int i = 0;
        public static void Main()
        {         
            // Create a timer with a Minute interval.
            aTimer = new System.Timers.Timer(60000);

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 1 Minute (60000 milliseconds).
            aTimer.Interval = 60000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.WriteLine(0 + ":" + 0);
            Console.ReadLine();
        }

        // Specify what you want to happen when the Elapsed event is 
        // raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {            
            j++;
            if (j == 60)
            {
                Console.WriteLine("break");
                j = 1;
                i = i + 1;
            }
            if (i == 24)
            {
                i = 0;
            }

            if (j % 5 != 0 || (j == 0))
            {                              
                Console.WriteLine(i + ":" + j);
            }
            else if (j % 5 == 0)
            {
                Console.WriteLine("break");
            }
        }
    }
于 2012-07-06T08:31:42.507 回答
0

我不确定您是否想使用实际的系统时间开始或只是程序执行开始以来的时间。自程序启动以来,我发布的解决方案使用时间。

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace ConsoleApplication1
{
   class Program
 {
    TimeSpan tt;
    static void Main(string[] args)
    {
        Program p = new Program();
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(p.run));
        t.Start();
        while (true) ;
    }

    void run()
    {
        tt=new TimeSpan(0,1,0);
        //Timer interval decides when even will be fired.
        Timer t = new Timer(60000);
        t.AutoReset = true;
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Start();

    }

    public void t_Elapsed(object sender, EventArgs e)
    {

        if (tt.Minutes % 5 == 0)
            Console.WriteLine("Break");
        Console.WriteLine(tt.Hours.ToString()+":"+tt.Minutes.ToString());
        tt = tt.Add(new TimeSpan(0, 1, 0));
    }
}
}
于 2012-07-06T04:49:29.683 回答