1.写一个控制台应用程序:
每1秒钟显示一个0到9的随机整数,并显示当前时间。
格式如下:
2 2009-6-22 14:30:22
1 2009-6-22 14:30:23
5 2009-6-22 14:30:24
6 2009-6-22 14:30:25
3 2009-6-22 14:30:26
1 2009-6-22 14:30:27
----------------------------------------------------------------
class Cula
{
public static void Print(object souce, ElapsedEventArgs e)
{
Random m = new Random();
DateTime b = DateTime.Now;
Console.WriteLine("{0,4}/t{1:G}", m.Next(10), b);
}
}
class Program
{
static void Main(string[] args)
{
Timer myTimer = new Timer(1000);
myTimer.Elapsed += new ElapsedEventHandler(Cula.Print);
myTimer.Start();
Console.ReadKey();
}
}
}
------------------------------------------------------------------------------
2.练习写一个控制台应用程序:随机产生10个0到50的不同的整数.
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace Test_000000
{
public delegate void ShowHandler(string mess);
public class Produce
{
private event ShowHandler ShowHandlerEvent;
private static Timer myTimer;
private static int temp=0;
public Produce()
{
myTimer = new Timer(1000);
myTimer.Elapsed += new ElapsedEventHandler(RanProduce);
}
void RanProduce(object source, ElapsedEventArgs e)
{
Random ran = new Random();
ShowHandlerEvent(ran.Next(50).ToString());
if (temp++ ==9)
myTimer.Stop();
}
public void Display(string mess)
{
Console.WriteLine(mess );
}
public void DoStart()
{
myTimer.Start();
}
static void Main()
{
Produce myProduce = new Produce();
myProduce.ShowHandlerEvent += new ShowHandler(myProduce.Display );
myProduce.DoStart();
Console.ReadKey();
}
}
}