您的位置:首页 > 编程语言 > C#

C#实验2.4

2016-03-25 16:28 302 查看
随机给出一个0-99之间的数字,然后猜数字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
guess();
}
static void guess()
{
Console.WriteLine("随机给出0-99的数字,猜猜这个数");
Random ran = new Random();
int k = ran.Next(0, 99);
int realNumber = k;
// Console.WriteLine("程序随机分配的值为:{0}", realNumber);
int yourGuess = 0;
Console.WriteLine("输入你的猜测:");
yourGuess = int.Parse(Console.ReadLine());
while (yourGuess != realNumber)
{
if (yourGuess > realNumber)
{
Console.WriteLine("猜大了,再输入你的猜测:");
yourGuess = int.Parse(Console.ReadLine());
}
else if(yourGuess<realNumber)
{
Console.WriteLine("猜小了,再输入你的猜测:");
yourGuess = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("猜对了!");
Console.ReadKey();
}
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: