Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How can I restart a while loop?

I was working on example code. What I want to do is to restart the while loop according to user input. So I tried to put the while loop under another while loop. However now it only repeats the initial loop result. Here is the code:

        Random numberGen = new Random();


        int roll = 0;

        int attempts = 0;

        string answer=("y");

        while(answer == ("y")) {

        while(roll !=6) {

            roll = numberGen.Next(1, 7);

            Console.WriteLine("you rolled " +roll);

            attempts++;

        }

        Console.WriteLine("it takes " + attempts + " to roll 6!");

        Console.Write("Restart? (y/n)");

        answer = Console.ReadLine();

        }

Sign In or Register to comment.