473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How do a loop this program? Learning CSharp

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");
}
}
}

Feb 27 '07 #1
3 5868
On Feb 27, 11:41 am, "Trev" <trevor.do...@gmail.comwrote:
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");

}
}

}
You could wrap the whole thing in a infinite loop, and then breaking
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

Feb 27 '07 #2
Trev <tr**********@gmail.comwrote:
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 - <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
Feb 27 '07 #3
On Feb 27, 6:54 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Trev <trevor.do...@gmail.comwrote:
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.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks, Working 100%

Feb 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
7
by: Francis Bell | last post by:
Hello, I've got a 25 line file with lines of data like this: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 The first field is the code that determines what to do. I need to loop through this...
3
by: el chupacabra | last post by:
Hi, I'm just learning Python....thanks in advance... Do you get out of this loop? Problem: When I type 'exit' (no quotes) the program doesn't quit the loop...it actually attemps to find entries...
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
15
by: John Salerno | last post by:
Ok, I've been reading an intro book to C# and I'm learning a lot of the basics. I can follow along with all the sample code and understand exactly what it's doing, but I know there's no way I'd be...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
1
by: adnan12 | last post by:
I want to write a short program using loop. I wrote one but it doesn’t working. I’m learning loops so it’s hard to find my mistakes. Here’s my code. Please define and correct where I made a mistake. ...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
7
by: =?Utf-8?B?RXZl?= | last post by:
I have the following sections defined in my app.config file: <configSections> <section name="1" type="System.Configuration.DictionarySectionHandler" /> <section name="2"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.