用函数将闹钟练一练

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace 用函数把闹钟习题练一练无返回值
{
    class Program
    {
        public void naozhong(DateTime dt,DateTime nz)   //定义一个函数输入当前时间和闹钟时间
        {
            for (int i = 0; i <= 1000; i++)
            {
                dt = dt.AddMinutes(1);  //在原有时间的基础上加上一分钟
                Console.WriteLine(dt.ToShortTimeString());   //dt.ToShortTimeString 把日期时间改成只有时间的格式
                if (dt.ToShortTimeString().Equals(nz.ToShortTimeString()))  //Equals()表示相等
                {
                    Console.WriteLine("到点了。");
                    Console.WriteLine("是否关闭闹钟(Y/N)");
                    string x = Console.ReadLine();
                    if (x == "Y")
                    {
                        break;
                    }
                    else
                    {
                        nz = nz.AddMinutes(5);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
                DateTime dt = DateTime.Now;
                Console.WriteLine(dt);
                DateTime nz = Convert.ToDateTime("2015-4-12 12:00");  //等号左边:输入闹钟的时间。  等号右边:转换成为日期时间的格式
                new Program().naozhong(dt,nz);  //调用naozhong函数,输入变量dt,nz来得到运算结果,此函数无返回值
                Console.ReadLine();
           
        }
    }
}

原文地址:https://www.cnblogs.com/lk-kk/p/4420598.html