/* 题目:猜数字
让程序产生1-100之间的随机数
让玩家重复猜测,直到猜对位置。
回答: “大了” “小了” “恭喜,猜对了,总共猜了多少次”
*/
已给出,创建随机数的代码
//创建一个随机数工具
Random number = new Random();
//产生在1到100以内的一个随机数
int q = number.Next(1,101);//左含右不含
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace P49
{
class Program
{
static void Main(string[] args)
{
/* 题目:猜数字
让程序产生1-100之间的随机数
让玩家重复猜测,直到猜对位置。
“大了”“小了”“恭喜,猜对了,总共猜了?次”
*/
//创建一个随机数工具
Random number = new Random();
//产生在1到100以内的一个随机数
int q = number.Next(1,101);//左含右不含
int count = 0;//现在内存里开一快地
int a;//先定义
do//先做一遍,再判断是否满足条件
{
Console.WriteLine("请输入一个猜测值:");
a = int.Parse(Console.ReadLine());//在这赋予值
count += 1;
if (a > q)
{
Console.WriteLine("大了");
}
else if (a < q)
{
Console.WriteLine("小了");
}
else
{
Console.WriteLine("恭喜你答对了,您一共猜测了{0}次。", count);
}
}
while (a != q);//满座while()里面的条件,才会开始下一循环
Console.ReadLine();
}
}
}