紧急求救.......(猜字游戏)
要求:
程序运行时自动产生一个1-100之间的随机数,让游戏者来猜这个数。
当从键盘接收到游戏者输入的数据后,程序给出的相应的提示信息(猜大了或是猜小了!),游戏者根据提示不断从键盘输入数据,直到猜中。
猜中则程序结束。
猜中分三种情况:
一次就猜中 superman
2-7次猜中 ordinary
8次包括8次以上猜中 fools
[解决办法]
Random r=new Random();int i=r.next(1,101);int input;int x=0;string result="猜对了";while(input!=i){x++;if(int.trypase(Console.ReadLine(),out input)){ result=input>i?"大了":input==i?"猜对了":"小了";Console.WriteLine(result);}}if(x==1){result="superman";}else if(x>1&&x<=7){result="ordinary";}else{result="fools";}Console.WriteLine(result);console.ReadKey();
[解决办法]
static void GuessNumber(){ Console.Write("Please input a number between 1 ~ 100, others to QUIT: "); var randomNumber = new Random().Next(1, 101); var lowerBound = 1; var upperBound = 100; var guessCount = 0; for (int i = 0; guessCount < 8 && (i = Convert.ToInt32(Console.ReadLine())) > 0; Console.WriteLine("Try it again. New Range: {0} ~ {1}", lowerBound, upperBound), guessCount++) { if (i == randomNumber) { Console.WriteLine("Congratulations! You got the number."); break; } else if (i > randomNumber) { upperBound = i; } else { lowerBound = i; } } if (guessCount == 8) { Console.WriteLine("Game Over. The Number is: {0}", randomNumber); }}
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RandomGame
{
class Program
{
static void Main(string[] args)
{
Random r = new Random(); //产生随机数
int num = r.Next(101); //接受随机数
int count = 0; //猜数字的统计数
while(true)
{
Console.WriteLine("请输入您要猜的数字0-100:");
String i = Console.ReadLine(); //接收从键盘输入的字符
int x = 1;
try
{
x = Convert.ToInt32(i); //将字符串类型转换为整形数据
}
catch(Exception e)
{
Console.WriteLine("方法main中捕获到:" + e.Message);
throw;
}
if (x > num)
{
// bool flag = true;
Console.WriteLine("您输入的数字太大了");
count++;
}
else if (x < num)
{
Console.WriteLine("您输入的数字太小了");
count++;
}
else
{
if(count<=1)
{
Console.WriteLine("您太有才了!");
}
else if (count >= 2 && count <= 6)
{
Console.WriteLine("您很聪明!");
}
else
{
Console.WriteLine("小同志,你还需要努力!");
}
break;
}
}
Console.ReadLine();
}
}
}