主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:241次

using System;


namespace GuessNumber
{
 ///


 /// Class1 的摘要说明。
 ///

 class Class1
 {
  ///
  /// 应用程序的主入口点。
  ///

  [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   //   {
   //    if(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 ;
   } 

  }

 

 }

}



<> <>
关键字词: