问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;namespaceNumberPuzzle{classProgram{///<summary>///NumPuzzle///^^**///</summary>///<paramname="args"></param>staticvoidMain(string[]args){stringnumPazzle=string.Empty;stringnumLength=string.Empty;intcount=0;intcountMax=0;Console.WriteLine("Howlongdoyouwant?[0<n<11]"Suggestion:4"");while(true){numLength=Console.ReadLine();if(IsNum(numLength)){countMax=Convert.ToInt32(numLength);if(countMax>10){Console.WriteLine("Re-inptdueton>10");continue;}break;}else{Console.WriteLine("Re-inpt,inputisnotanum:");continue;}}while(count<countMax){stringstrA=GetNum();if(numPazzle.IndexOf(strA)!=-1){continue;}numPazzle+=strA;count++;}while(true){stringinput=string.Empty;stringresults=string.Empty;Console.WriteLine("Inputwhatyouguess:");input=Console.ReadLine();if(!IsNum(input)){Console.WriteLine("Re-inpt,inputisnotanum:");continue;}if(input.Length!=countMax){Console.WriteLine("Thelengthofinputiserror");continue;}if(IsDup(input)){Console.WriteLine("Inputisadupnum");continue;}results=CompareNum(input,numPazzle);if(results.Split('-')[0].Equals(numPazzle.Length.ToString()))break;Console.WriteLine("Results:A-{0}B-{1}",results.Split('-')[0],results.Split('-')[1]);}Console.WriteLine("Win!Thenumis{0}",numPazzle);Console.ReadKey();}publicstaticstringGetNum(){RandomsSeed=newRandom();Randomseed=newRandom(sSeed.Next());returnseed.Next(10).ToString();}publicstaticstringCompareNum(stringactualStr,stringexpectedStr){inta=0;intb=0;stringresults=string.Empty;for(inti=0;i<actualStr.Length;i++){if(expectedStr.IndexOf(actualStr[i])!=-1){b++;}if(expectedStr[i].Equals(actualStr[i])){a++;b--;}}results=a.ToString()+"-"+b.ToString();returnresults;}publicstaticboolIsDup(stringinput){boolresult=false;foreach(charaStrininput){if(input.IndexOf(aStr)!=input.LastIndexOf(aStr)){result=true;break;}}returnresult;}publicstaticboolIsNum(stringnumInput){boolresult=false;Regexreg=newRegex(@"^-?d+$");result=reg.IsMatch(numInput);returnresult;}}}