C # version Guess number

Source: Internet
Author: User

Using System;


Namespace Guessnumber
{
<summary>
Summary description of the CLASS1.
</summary>
Class Class1
{
<summary>
The main entry point for the application.
</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;
}

}

}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.