using System;
namespace GuessNumber
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class Class1
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
GuessGame myGame=new GuessGame();
while(myGame.Play().ToUpper()=="Y")
{
}
}
}
class GuessGame
{
private int ResultNumber=1;
int UserNumber=0;
int Chance=0;
string Flag="";
string TempStr="";
public GuessGame()
{
Init();//
}
public string Play()
{
Console.WriteLine("Input a number:");
TempStr=Console.ReadLine();
while(!IsNum(TempStr))
{
TempStr=Console.ReadLine();
}
UserNumber=Convert.ToInt16(TempStr);
while(ResultNumber!=UserNumber)
{
if(ResultNumber>UserNumber)
Flag="samll";
else
Flag="big";
Chance+=1;
Console.WriteLine("Too {0} !!!You have guessed {1} times,Input a number again:",Flag,Chance);
TempStr=Console.ReadLine();
while(!IsNum(TempStr))
{
}UserNumber=Convert.ToInt16(TempStr);
}
Console.Write("You did it! {0} times",Chance+1);
Console.Write("Play again? Y:N ");
return Console.ReadLine();
}
// private override string ReadLine()
// {
//
// }
private void Init()
{
ResultNumber = CreateNumber();
Console.WriteLine("The number is between 1-100");
}
private int CreateNumber()
{
Random myRan=new Random();
return myRan.Next(0,10);
}
private bool IsNum(string Str)
{
// for(int i=0;i<Str.Length;i++)
// {
// if(Convert.ToInt16(Str.Substring(i,1))<Convert.ToInt16("0")||Convert.ToInt16(Str.Substring(i,1))>Convert.ToInt16("9"))
// {
// Console.WriteLine("Fomat error,Input a NUMBER");
// return false;
// }
// }
try
{
Convert.ToDouble(Str);
return true;
}
catch
{
Console.WriteLine("Fomat error,Input a NUMBER");
return false ;
}
}
}
}