码迷,mamicode.com
首页 > 其他好文 > 详细

猜数字游戏的控制台代码

时间:2017-10-02 12:41:56      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:bsp   amp   换行   enter   class   今天   except   int   清除   

  今天修改了以前写的猜数字游戏的代码,之前的代码一大坨,可读性太差,所以做了些修改,优化了结构,也正好消除了之前的一个问题。感兴趣的朋友可以看看,以下是代码部分:

  1 using System;
  2 using System.Linq;
  3 
  4 namespace 猜数字游戏_0_100_
  5 {
  6     class Program
  7     {
  8         static void Main(string[] args)
  9         {
 10             GuessMethod(0, 100);
 11 
 12             PlayAgain();
 13         }
 14 
 15         private static void GuessMethod(int lowerLimit, int upperLimit)
 16         {
 17             Random rand = new Random();
 18             int randomNumber = rand.Next(lowerLimit, upperLimit);
 19 
 20             int guessTimes = 1;
 21 
 22             Console.WriteLine("欢迎来到猜数字游戏,数字范围为 {0,-2} - {1,-2},按Enter键开始", lowerLimit, upperLimit);
 23             Console.ReadLine();
 24 
 25             for (guessTimes = 1; guessTimes <= 10; guessTimes++)
 26             {
 27                 int guessNumber = InputNumber(guessTimes, lowerLimit, upperLimit);
 28 
 29                 if (guessNumber == randomNumber)
 30                 {
 31                     Console.WriteLine("恭喜你猜对了!正确的答案为{0}!\r\n", randomNumber);
 32                     break;
 33                 }
 34                 else if (guessNumber < randomNumber)
 35                 {
 36                     lowerLimit = guessNumber + 1;
 37                     Console.WriteLine("真遗憾,猜小了点,加油哦!\r\n");
 38                 }
 39                 else if (guessNumber > randomNumber)
 40                 {
 41                     upperLimit = guessNumber - 1;
 42                     Console.WriteLine("真遗憾,猜大了点,加油哦!\r\n");
 43                 }
 44             }
 45 
 46             if (guessTimes <= 5)
 47                 Console.WriteLine("真厉害!!!仅用了{0}次就猜中了!\r\n", guessTimes);
 48             else if (guessTimes > 5 && guessTimes <= 10)
 49                 Console.WriteLine("用了{0}次才猜中,下次再接再厉!\r\n", guessTimes);
 50             else if (guessTimes > 10)
 51                 Console.WriteLine("10次都没猜中,你输了!下次加油哦!\r\n");
 52         }
 53 
 54         private static int InputNumber(int guessTimes, int lowerLimit, int upperLimit)
 55         {
 56             int guessNumber;
 57         retry: try
 58             {
 59                 Console.WriteLine("第{0}次猜,范围为 {1,-2} - {2,-2} ;", guessTimes, lowerLimit, upperLimit);
 60                 Console.Write("请输入你所猜的数字:");
 61                 string guessNumberString = Console.ReadLine();
 62                 if (string.IsNullOrEmpty(guessNumberString))
 63                     throw new Exception("您未输入任何数,请重新输入!");
 64                 else if (guessNumberString.Length > 4)
 65                     throw new Exception("您的输入超出界限,请重新输入!");
 66                 else if (!(int.TryParse(guessNumberString, out guessNumber)))
 67                     throw new Exception("您输入的数字有问题,请重新输入!");
 68                 else if (guessNumber < lowerLimit || guessNumber > upperLimit)
 69                     throw new Exception("您输入的数字超出界限,请重新输入!");
 70             }
 71             catch (Exception e)
 72             {
 73                 Console.WriteLine(e.Message + "\r\n");
 74                 goto retry;
 75             }
 76             return guessNumber;
 77         }
 78 
 79         private static void PlayAgain()
 80         {
 81             Console.Write("是否再来一局? Y(是)/N(否):");
 82             string jugment = Console.ReadLine();
 83             string[] judgment1 = new string[] { "", "Y", "y", "YES", "Yes", "yes" };
 84             string[] judgment2 = new string[] { "", "N", "n", "NO", "No", "no" };
 85             while (!(judgment1.Contains(jugment) || judgment2.Contains(jugment)))
 86             {
 87                 Console.Write("是否再来一局? Y(是)/N(否):");
 88                 jugment = Console.ReadLine();
 89             }
 90             if (judgment1.Contains(jugment))
 91             {
 92                 Console.Write("是否想清除之前的记录? Y(是)/N(否):");
 93                 jugment = Console.ReadLine();
 94                 while (!(judgment1.Contains(jugment) || judgment2.Contains(jugment)))
 95                 {
 96                     Console.Write("是否想清除之前的记录? Y(是)/N(否):");
 97                     jugment = Console.ReadLine();
 98                 }
 99                 if (judgment1.Contains(jugment))
100                     Console.Clear();
101                 else
102                     Console.WriteLine("==================================================" +
103                         "\r\n==================================================");
104 
105                 Main(null);
106             }
107         }
108     }
109 }

 

说明:

1. \r\n 是换行符;

2. for 循环执行break后,i++ 不会执行;

3. 当 if 后的执行语句只有一条,可以省略“{ }”;

4. WriteLine 中的索引号 {0, -2} 就是普通的索引号 {0} ,-2 表示索引的内容至少占位 2 个字符,“-” 号表示左对齐,无符号则是右对齐。

 

个人不太喜欢写注释【不是不写(lll¬ω¬)】,觉得代码自己写的清晰就好。

欢迎留言!??

猜数字游戏的控制台代码

标签:bsp   amp   换行   enter   class   今天   except   int   清除   

原文地址:http://www.cnblogs.com/jling/p/7619942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!