【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《C#程序--猜数字游戏》,欢迎阅读!
class Program {
static void Main(string[] args) {
bool w = true; while (w) {
Random r = new Random(); int num1 = r.Next(0, 100); bool a = true; int i = 0; while (a) {
Console.WriteLine("请输入您猜的数字: "); int num2 = int.Parse(Console.ReadLine()); if (num2 >= 0 & num2 <= 100) {
i = i + 1;
if (num2 > num1) {
Console.WriteLine("太大!请重新输入!"); a = true;
Console.WriteLine("这是第{0}次猜测! ", i); } else {
if (num2 < num1) {
Console.WriteLine("太小!请重新输入!"); a = true;
Console.WriteLine("这是第{0}次猜测! \n",i); } else {
Console.WriteLine("这是第{0}次猜测! \n", i);
Console.WriteLine("恭喜您猜对了!这个数是:{0}",num1); Console.WriteLine("是否继续猜数?1是,2否"); int b = int.Parse(Console.ReadLine()); if (b == 1) {
a = false; w = true; } else {
a = false; w = false; } } } } else {
Console.WriteLine("输入无效!请重新输入!"); w =true; } } }
} }
本文来源:https://www.wddqxz.cn/34592c2a7375a417866f8f72.html