wout hiemstrra
wout hiemstrra

Reputation: 11

How do i continue to the next statement after the while loop is done?

So what do i have to do after the first While loop is corect and it continues to the second one?.. hope u guys can give me a hand :) Iam a beginning c# programmer btw ;P

here is my code :

            bool correctAwnser = true;

            Console.WriteLine("You selected Easy mode!" + "\n" + "First riddle..");


            while (correctAwnser)
            {


                Console.WriteLine("The more you take, The more you leave behind. What am I?");
                if (Console.ReadLine() == "Footsteps")
                {
                    Console.WriteLine("That is correct! that is 5 points!");
                    points = easyPoints;
                    Console.WriteLine("You have " + points + " points");
                    correctAwnser = false;
                }
                else
                {
                    Console.WriteLine("Sorry that is not correct!");
                }
            }

            while (correctAwnser)
            {
                Console.WriteLine("Mr.Smith has 4 daughters. Each of his daugthers has a brother. How many children does Mr.Smith has?");
                if(Console.ReadLine() == "5 children")
                {
                    Console.WriteLine("That is correct. you gained 5 points!");
                    points = easyPoints + 5;
                    Console.WriteLine("You have a total of  " + easyPoints + " points");
                    correctAwnser = false;  
                }
                else
                {
                    Console.WriteLine("Sorry that is not correct!");
                }
            }

Upvotes: 1

Views: 90

Answers (2)

mybirthname
mybirthname

Reputation: 18127

You can re work your code like this:

    public static void Main(string[] args)
    {
        int points = 0;

        string question1 = "The more you take, The more you leave behind. What am I?";
        string answer1 = "Footsteps";
        points += Question(question1, answer1);

        Console.WriteLine("You have " + points + " points");

        string question2 = "Mr.Smith has 4 daughters. Each of his daugthers has a brother. How many children does Mr.Smith has?";
        string answer2 = "5 children";
        points+= Question(question2, answer2);

        Console.WriteLine("You have " + points + " points");

    }

    public static int Question(string question, string answer)
    {
        Console.WriteLine(question);

        while (Console.ReadLine() != answer)
        {
            Console.WriteLine("Sorry that is not correct!");
            Console.WriteLine(question);
        }

        return 5;
    }

Upvotes: 0

Alex F
Alex F

Reputation: 223

Set your boolean back to true between the loops. This is because your boolean correctAwnser is set to false during the first loop, and remains false when you get to the second loop. Simply switching it back to true will do it!

Upvotes: 1

Related Questions