计算机随机生成一个数10以内的整数,猜数的大小,如果猜的数字不相等,则提示,直到猜对为止,只有3次机会
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessNumber
{
internal class Program
{
static void Main(string[] args)
{
const int Min = 1;
const int Max = 10;
const int Guessnum = 3;
const string Quit = "Q";
Random rand = new Random();
int corrent;
string inputString=null;
int input=0;
int num = 1;
corrent = (int)Math.Round(rand.NextDouble() * Max);
Console.WriteLine("猜一个从{0}到{1}的数,输入{2}结束", Min, Max, Quit);
for (; num <= Guessnum; )
{
Console.WriteLine("请输入一个从{0}到{1}的数", Min, Max);
inputString = Console.ReadLine();
if (inputString == Quit )
{
Console.WriteLine("游戏提前结束");
break;
}
try
{
input = Convert.ToInt16(inputString);
}
catch
{
continue;
}
if (input>corrent&&num< Guessnum)
{
num++;
Console.WriteLine("你输入的值猜大了。");
}
else if(input<corrent&&num< Guessnum)
{
num++;
Console.WriteLine("你猜的数猜小了");
}
else if(input==corrent)
{
Console.WriteLine("猜对了,游戏成功!");
break;
}
else
{
Console.WriteLine("猜错了,该值是{0},游戏失败!",corrent);
break;
}
}
Console.ReadKey();
}
}
}