猜数字游戏(简单C#实现)

来源:互联网 发布:win7网络设置 编辑:程序博客网 时间:2024/05/07 16:34
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 猜数字{    class Program    {        static void Main(string[] args)        {            new Program().play();        }        void play()        {            System.Random r = new System.Random(); //生成随机种子            int j = r.Next(1, 100);            int i;            int n = 1;            System.Console.WriteLine("请猜猜随机数是多少?(1-100)");            do            {                i = int.Parse(System.Console.ReadLine());                if (i == j)                    System.Console.WriteLine("恭喜您在第{0}次猜对了!", n);                else                {                    if (i < j)                    {                        System.Console.WriteLine("第{0}次,猜小了", n);                        System.Console.WriteLine("再试试:");                    }                    else                    {                        System.Console.WriteLine("第{0}次,猜大了", n);                        System.Console.WriteLine("再试试:");                    }                }                n++;            } while (i != j);            System.Console.ReadLine();        }    }}

0 0
原创粉丝点击