用C#写的一个猜数字的游戏

游戏的规则是由计算机产生一个随机数,然后与用户输入的数字比较,然后给出大小提示,直到猜对为止。
  1 using  System;
  2 namespace  guess
  3 {
  4    class readyguess                           //用户输入参数
  5    {
  6        public int formatinput()
  7        {
  8            int i = 0;
  9            try
 10            {
 11                i = Convert.ToInt16(Console.ReadLine());
 12                if (i == 0)
 13                    System.Environment.Exit(-1);
 14                return i;
 15            }

 16            catch (FormatException)
 17            {
 18                Console.WriteLine("输入参数有误,请重新输入:");
 19                return 0;
 20            }

 21
 22                             
 23        }

 24
 25    
 26  
 27        public int Rannumber()    //由计算机产生一个随机数(0~100)的类
 28        {   
 29            Random Ran;
 30            Ran = new Random();
 31           int i = Convert.ToInt32(Ran.NextDouble() * 100);
 32            return i;
 33        }

 34
 35        public void Commentguess(int count)   //根据游戏者的次数给出评价
 36        {
 37            
 38            switch (count)       
 39            {
 40                case 1:
 41                case 2:
 42                case 3:
 43                case 4:
 44                case 5:
 45                case 6:
 46                    Console.WriteLine("而且你非常聪明!猜了{0}次就对了!", count);
 47                    break;
 48                case 7:
 49                case 8:
 50                case 9:
 51                case 10:
 52                case 11:
 53                    Console.WriteLine("你还行!猜了{0}次!", count);
 54                    break;
 55                default:
 56                    Console.WriteLine("不过你真笨,猜了{0}次才对!", count);
 57                    break;
 58            }

 59        }

 60        public void right()       //版权所有
 61        {
 62           Console.WriteLine("以上程序由Simon.Yang编写,谢谢使用。");
 63           Console.WriteLine(    "[email protected]");
 64        }

 65
 66    }
  
 67    class test
 68    {
 69        static void Main(string[] args)
 70        {
 71            int y;
 72            int i, j,count=0;
 73 loop:    Console.WriteLine("请输入一个0到100之间的整数,退出请按0");
 74            readyguess guessnumber =new readyguess();
 75           
 76           i= guessnumber.Rannumber();
 77            do
 78            {
 79                count++;
 80                j = guessnumber.formatinput();
 81            if (j != 0)
 82              {
 83                    if (i > j)
 84                        Console.WriteLine("请输入更大的数。");
 85                    if (i < j)
 86                        Console.WriteLine("请输入更小的数。");
 87               }

 88
 89            }

 90            while (i != j);
 91            if (i == j)             
 92           Console.WriteLine("恭喜你!你猜对了");
 93            guessnumber.Commentguess(count);
 94            guessnumber.right();
 95            Console.WriteLine("要继续吗?请按1继续,按0退出");
 96          if(( y= Convert.ToInt32(Console.ReadLine()))==1)
 97            goto loop;
 98        }

 99    }

100}

101
102
103

你可能感兴趣的:(C#)