游戏内容概述:计算机想到一个 0 到 100之间的随机数作为目标数,让你来猜它是几。计算机会根据你的输入给出相应的提示: 1、如果输入的数比目标数小,计算机会提示输入的数太小了; 2、如果输入的数比目标数大,计算机会提示输入的数太大了;3、如果输入的数与目标数相等,计算机会提示你猜中了。
C#VS2008工程源代码的下载地址为:https://download.csdn.net/download/mr_frank_xie/10846067
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PredictNumberGame
{
class Program
{
private static int TargetNumber; //目标数
private static Random RandomNum = new Random();
private static int UserInputInteger; //用户猜的数
private static string UserInputString = string.Empty; //用户输入是否继续游戏
private static bool IsGameContinue = true; //游戏是否继续的标志位
private static string ExceptionType = string.Empty; //异常类型
static void Main(string[] args)
{
int reward; //是否出现奖励提示语的标志位,连续完成了3次以上10次以内的某一次游戏结束后,会出现奖励提示
bool surprise = true; //是否第一次就猜中了的标志位
int count = 0; //游戏连续运行次数
reward = RandomNum.Next(3, 10);
do
{
TargetNumber = RandomNum.Next(0, 100);
Console.WriteLine("I'm thinking a number that greater than or equal to 0 and less than 100!");
Console.Write("Try to guess the number I'm thinking of:");
do
{
InputNumber();
if (UserInputInteger < TargetNumber)
{
surprise = false;
Console.Write("Too low! Guess again:");
continue;
}
else if (UserInputInteger > TargetNumber)
{
surprise = false;
Console.Write("Too high! Guess again:");
continue;
}
else
{
count++;
Console.Write("That's it! ");
if (surprise) //如果用户第一次就猜中了,输出奖励提示语
{
Console.WriteLine("You are so clever! I'm so proud of you!");
}
if (reward == count) //如果用户连续多次(次数在[3,10)范围内随机产生)完成游戏,输出奖励提示语
{
Console.WriteLine("Well done! You did a great job!");
}
InputYesOrNo();
break;
}
} while (true);
} while (IsGameContinue);
Console.WriteLine("Thanks for playing!");
Console.WriteLine("Press any key to exit!");
Console.ReadKey();
}
/// <summary>
/// 用户输入猜的数
/// </summary>
private static void InputNumber()
{
try
{
UserInputInteger = Convert.ToInt32(Console.ReadLine());
}
catch (System.Exception ex)
{
ExceptionType = ex.GetType().ToString();
switch (ExceptionType)
{
case "System.FormatException": //用户输入的字符串中包含0-9以外的字符的异常处理
{
Console.WriteLine("Please input a integer!");
Console.Write("Try to guess the number I'm thinking of:");
InputNumber();
break;
}
case "System.OverflowException": //用户输入的数字超出了System.Int32所能表示的范围异常处理
{
Console.WriteLine("Please input a number that greater than or equal to 0 and less than 100!");
Console.Write("Try to guess the number I'm thinking of:");
InputNumber();
break;
}
default:
{
Console.WriteLine(ExceptionType);
InputNumber();
break;
}
}
}
//如果用户输入的数字不是在[0,100)范围内,输出提示让用户重新输入
if (UserInputInteger < 0 || 100 <= UserInputInteger)
{
Console.WriteLine("Please input a number that greater than or equal to 0 and less than 100!");
Console.Write("Try to guess the number I'm thinking of:");
InputNumber();
}
}
/// <summary>
/// 用户输入游戏是否继续的相应字符串
/// </summary>
private static void InputYesOrNo()
{
Console.Write("Would you like to play again? (yes/no)");
UserInputString = Console.ReadLine().ToLower();
if (string.Equals(UserInputString, "yes"))
{
IsGameContinue = true;
}
else if (string.Equals(UserInputString, "no"))
{
IsGameContinue = false;
}
else //如果用户输入的不是"yes"或者"no",让用户重新输入
{
Console.WriteLine("Please input 'yes' or 'no'!");
InputYesOrNo();
}
}
}
}