日期:2014-05-17 浏览次数:21132 次
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();
}