Home All Groups Group Topic Archive Search About

How do a loop this program? Learning CSharp

Author
27 Feb 2007 4:41 PM
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");


        }
    }
}

Author
27 Feb 2007 4:52 PM
rowe_newsgroups
Show quote Hide quote
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
Are all your drivers up to date? click for free checkup

Author
27 Feb 2007 4:54 PM
Jon Skeet [C# MVP]
Trev <trevor.do***@gmail.com> wrote:
Show quoteHide quote
> 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
Author
27 Feb 2007 6:20 PM
Trev
On Feb 27, 6:54 pm, Jon Skeet [C# MVP] <s***@pobox.com> wrote:
Show quoteHide quote
> 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.comhttp://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%

Bookmark and Share