while循环完成后如何继续下一个语句?

时间:2016-11-10 20:53:47

标签: c# while-loop

那么在第一个While循环是corect之后我还要做什么呢?它继续到第二个?希望你们能帮我一臂之力:) 我是一个开始c#programmer btw; P

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

在循环之间将布尔值设置回true。这是因为在第一个循环期间你的boolean correctAwnser被设置为false,并且当你到达第二个循环时它仍然是false。只需将其切换回true就可以了!

答案 1 :(得分:0)

您可以像这样重新编写代码:

    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;
    }