How do a loop this program? Learning CSharp

T

Trev

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


}
}
}
 
R

rowe_newsgroups

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
 
J

Jon Skeet [C# MVP]

Trev said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top