1. 程式人生 > >C#-猜數遊戲-控制檯(隨機數)---ShinePans

C#-猜數遊戲-控制檯(隨機數)---ShinePans

//guess the number 
class program
{
    static void Main()
    {
        new program().Play();
    }
    void Play()
    {
        System.Random r = new System.Random();
        int j = r.Next(1, 100);
        int i;
        int n = 1;
        System.Console.Write("Please Guess The Number:(1-100):");
        do
        {
            i = int.Parse(System.Console.ReadLine());
            if (i == j)
            {
                System.Console.WriteLine("Congratulations!,You win at {0} times when you try", n);
            }
            else
            {
                if (i < j)
                {
                    System.Console.WriteLine("The {0} times,too small", n);
                    System.Console.Write("Try again;");
                }
                else
                {
                    System.Console.WriteLine("The {0} times,too big", n);
                    System.Console.Write("Try again:");
                }
            }
            n++;
        } while (i != j);
        System.Console.ReadLine();
    }
}