PC Review


Reply
Thread Tools Rate Thread

How do a loop this program? Learning CSharp

 
 
Trev
Guest
Posts: n/a
 
      27th Feb 2007
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");


}
}
}

 
Reply With Quote
 
 
 
 
rowe_newsgroups
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 27, 11:41 am, "Trev" <trevor.do...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Feb 2007
Trev <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Trev
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 27, 6:54 pm, Jon Skeet [C# MVP] <s...@pobox.com> wrote:
> Trev <trevor.do...@gmail.com> wrote:
> > 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%

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to escape text in CSharp console program Alan Mosley Microsoft Dot NET Framework 2 22nd May 2008 10:00 AM
How to escape text in CSharp console program Alan Mosley Microsoft C# .NET 3 22nd May 2008 10:00 AM
Loop learning =?Utf-8?B?SG93YXJk?= Microsoft Excel Programming 3 1st Mar 2007 11:18 PM
problem with learning program pierre Windows XP Accessibility 0 2nd Oct 2005 03:29 PM
Run PPC Program by csharp? RC Microsoft Dot NET Compact Framework 2 14th Feb 2005 05:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 AM.