2016-11-10 560 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。只需將其切換回真即可實現!