2016-11-10 564 views
1

所以我有什么第一后做while循环corect并继续第二个..希望能跟大家能够给我一个手:) 荫一开始C#程序员顺便说一句,P如何在while循环完成后继续执行下一个语句? ?

这里是我的代码:

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

环结合为一,然后设置correctAwnser假 –

+1

创建像AskAndWaitForCorrectAnswer功能(问题的答案),并调用它,你需要尽可能多的时间。 – smibe

回答

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

设置你的布尔回真正的环之间。这是因为您的布尔correctAwnser在第一个循环期间设置为false,并且在到达第二个循环时保持为false。只需将其切换回真即可实现!