2016-11-10 47 views
1

ループがcorectであり、それは第二1を継続している間それでは、私が最初にした後に行う必要があります。.. uの人は私の手を与えることができることを望む:) Iamはところで始まるC#のプログラマーは、?Pwhileループが終了した後、次のステートメントを続行するにはどうすればよいですか?

こちら私のコードは次のとおりです。

  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

ループを1つに結合し、correctAwnserをfalseに設定します。 –

+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

はループ間trueに戻すあなたのブール値を設定します。これは、ブーリアンのcorrectAwnserが最初のループではfalseに設定され、2番目のループに到達するとfalseのままであるためです。本当にそれを元に戻すだけです!

関連する問題