|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do a loop this program? Learning CSharpI'm learning C# and trying to write the program below, everything works fine until the last section: Console.Write("Would you like to play again y/n: "); again = Console.ReadLine(); if (again.ToLower() == "y") { ???? } else Console.WriteLine("GoodBye"); How do I loop the program to start again, I tried copying the whole program into the if statement but this doesn't work any ideas? static void Main(string[] args) { Random generator = new Random(); int RandomNumber = generator.Next(1,100); int score = 1; int Guess = 0; string again; while (Guess != RandomNumber) { Console.Write("Guess a number from 1 to 100: "); Guess = Convert.ToInt32(Console.ReadLine()); if (Guess == RandomNumber) Console.WriteLine("Your Correct. It took you " + score + " Tries"); else if( Guess > RandomNumber) Console.WriteLine("To High"); else Console.WriteLine("To Low"); score++; } Console.Write("Would you like to play again y/n: "); again = Console.ReadLine(); if (again.ToLower() == "y") { ???? } else Console.WriteLine("GoodBye"); } } }
Show quote
Hide quote
On Feb 27, 11:41 am, "Trev" <trevor.do***@gmail.com> wrote: You could wrap the whole thing in a infinite loop, and then breaking> Hi, > > I'm learning C# and trying to write the program below, everything > works fine until the last section: > > Console.Write("Would you like to play again y/n: "); > again = Console.ReadLine(); > > if (again.ToLower() == "y") > { > ???? > } > else > Console.WriteLine("GoodBye"); > > How do I loop the program to start again, I tried copying the whole > program into the if statement but this doesn't work any ideas? > > static void Main(string[] args) > { > > Random generator = new Random(); > int RandomNumber = generator.Next(1,100); > int score = 1; > int Guess = 0; > string again; > > while (Guess != RandomNumber) > { > Console.Write("Guess a number from 1 to 100: "); > Guess = Convert.ToInt32(Console.ReadLine()); > > if (Guess == RandomNumber) > Console.WriteLine("Your Correct. It took you " + score + " > Tries"); > else if( Guess > RandomNumber) > Console.WriteLine("To High"); > else Console.WriteLine("To Low"); > score++; > > } > > Console.Write("Would you like to play again y/n: "); > again = Console.ReadLine(); > > if (again.ToLower() == "y") > { > ???? > } > else > Console.WriteLine("GoodBye"); > > } > } > > } the loop when you want to exit. Something like: while (true) { //Your other code here Console.Write("Would you like to play again y/n: "); again = Console.ReadLine(); if (again.ToLower() != "y") { Console.WriteLine("GoodBye"); break; } } Thanks, Seth Rowe Trev <trevor.do***@gmail.com> wrote:
Show quoteHide quote > I'm learning C# and trying to write the program below, everything Well, you want to play *while* the user is interested, right...> works fine until the last section: > > Console.Write("Would you like to play again y/n: "); > again = Console.ReadLine(); > > if (again.ToLower() == "y") > { > ???? > } > else > Console.WriteLine("GoodBye"); > > > How do I loop the program to start again, I tried copying the whole > program into the if statement but this doesn't work any ideas? Does that give you enough of a hint? -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too On Feb 27, 6:54 pm, Jon Skeet [C# MVP] <s***@pobox.com> wrote:
Show quoteHide quote > Trev <trevor.do***@gmail.com> wrote: Thanks, Working 100%> > I'm learning C# and trying to write the program below, everything > > works fine until the last section: > > > Console.Write("Would you like to play again y/n: "); > > again = Console.ReadLine(); > > > if (again.ToLower() == "y") > > { > > ???? > > } > > else > > Console.WriteLine("GoodBye"); > > > How do I loop the program to start again, I tried copying the whole > > program into the if statement but this doesn't work any ideas? > > Well, you want to play *while* the user is interested, right... > Does that give you enough of a hint? > > -- > Jon Skeet - <s***@pobox.comhttp://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too
Other interesting topics
How can i have multiple base classes.
dynamic length arrays Overriding the add method of a generic dictionary Marshaling variable length arrays Interlocked on a string? "Logon failure" using Invoke("SetPassword"... Xml Read Single Line at a time using C# How to add date in access database Showing HTML page in a windows application Looking for some sample codes in C# |
|||||||||||||||||||||||