2017-03-06 6 views
1

多肢選択プログラムで問題が発生しているようです。私はwhileループで試行する方法を理解していないようです。たとえば、私はあなたに2つの試みを与え、最初の質問が間違っていると言うことができます。それで、あなたは同じ質問でもう一度試してみましょう。私はwhileループを使う必要があると思います。実装する方法を理解できないようです。また、100でない場合にプログラムを再起動できる方法はありますか?C#複数の選択肢の試行と再起動

string First; 
     int score = 0; 
     string Second; 


     TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; 


     Console.Write("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 

     First = Console.ReadLine(); 

     switch (First) 

     { 

      case "B": 

       Console.WriteLine("You entered the correct answer!"); 



       break; 

      case "A": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      case "C": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      case "D": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      default: 


       Console.WriteLine("You did not enter a correct answer."); 

       break; 

     } 
     if (First == "B") 
     { 
      score = score + 50; 
      Console.WriteLine("Correct!\n" + " score:" + score + "\n"); 
     } 
     else 
     { 
      Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
     } 
     Console.Write("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 

     Second = Console.ReadLine(); 

     switch (Second) 

     { 

      case "A": 

       Console.Write("You entered the correct answer!"); 



       break; 

      case "B": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      case "C": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      case "D": 

       Console.WriteLine("You entered the wrong answer."); 

       break; 

      default: 


       Console.WriteLine("You did not enter a correct answer."); 

       break; 

     } 
     if (Second == "A") 
     { 
      score = score + 50; 
      Console.WriteLine("Correct!\n" + " score:" + score + "\n"); 
     } 
     else 
     { 
      Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
     } 
+0

正解が1つしかなく残りが間違っているので、 'switch/case'を' if'で置き換えることができます:if(First == 'B'){// Right} else { /間違った} ' – itsme86

+0

それぞれの質問を入れてみてください(つまり、質問と入力文とswitch文を読むこと)。 falseを返す場合は、再度メソッドを呼び出します。また、@ itsme86が述べているように、すべての可能な組み合わせをテストするためにswitch文が本当に必要なわけではありません。入力が正しいかどうかだけは興味があります。 – auburg

答えて

0

他の人が述べたように、コードの柔軟性を高め、不要な重複を避けるために、コードのアプローチを変更する必要があります。しかし、もし私があなたの構造に従っていたら、整数変数を使って残った試行回数を保存し、間違った解答を出すたびに減算することができます。条件は左試行回数が> 0の例であることで、whileループ内の各質問のコード包み込ん:あなたはオブジェクト指向と、そのようなことをしたい場合はitsme86の答え@

string First; 
int score = 0; 
int attempts = 2; //our integer variable 
string Second; 
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; 

while (attempts > 0) 
{ 
    //First question 
    Console.WriteLine("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); //use WriteLine instead of Write, for console clarity 
    First = Console.ReadLine(); 
    switch (First.ToUpper()) //accept lowercase inputs 
    { 
     case "B": Console.WriteLine("You entered the correct answer!"); break; 
     case "A": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break; //same logic 
     default: Console.WriteLine("You did not enter a correct answer."); break; 
    } 
    if (First.ToUpper() == "B") 
    { 
     score = score + 50; 
     Console.WriteLine("Correct!\n" + " score:" + score + "\n"); 
     break; //force exit loop, onto the next question 
    } 
    else 
    { 
     Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
     attempts--; //decrease number of attempts left 
    } 
} 
while(attempts > 0) 
{ 
    //Second question 
    Console.WriteLine("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 
    Second = Console.ReadLine(); 

    switch (Second.ToUpper()) //accept lowercase inputs 
    { 
     case "A": Console.Write("You entered the correct answer!"); break; 
     case "B": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break; 
     default: Console.WriteLine("You did not enter a correct answer."); break; 
    } 
    if (Second.ToUpper() == "A") 
    { 
     score = score + 50; 
     Console.WriteLine("Correct!\n" + " score:" + score + "\n"); 
     break; //you win! 
    } 
    else 
    { 
     Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
     attempts--; //decrease number of attempts left 
    } 
} 
+0

これは実際に私が見たいと思っていたものです!アドバイスをいただきありがとうございました。私は本当に読み書きしやすくするために、私の複製の問題に取り組む必要があります。 – Shade

1

正しい答えを確認するアプローチを変更すると、より明確にすることができます。私は、質問情報を保持するクラスを作成することをお勧め:

class Question 
{ 
    public string Text { get; private set; } 
    public string Answer { get; private set; } 

    public Question(string text, string answer) 
    { 
     Text = text; 
     Answer = answer; 
    } 
} 

その後、あなたの代わりにそれぞれに対して大きな分岐構造を作成する必要の質問のリストを作成できます。

List<Question> questions = new List<Question>(); 
questions.Add(new Question("Where is the capital of...", "B")); 
questions.Add(new Question("Where is Walt Dis...", "A")); 

今すぐ質問をすること、リストをループすることができます。それはあなたの二度目のチャンスの機能を追加することも簡単です。

foreach (Question question in questions) 
{ 
    bool answeredCorrectly = false; 

    for (int i = 0; i < 2; ++i) // up to 2 chances 
    { 
     Console.Write(question.Text); 
     string answer = Console.ReadLine(); 
     if (answer == question.Answer) 
     { 
      Console.WriteLine("You answered correctly!"); 
      answeredCorrectly = true; 
      break; // Make sure we break out of the for loop so we don't ask a second time 
     } 
     else 
     { 
      Console.WriteLine("That's not correct."); 
     } 
    } 

    if (answeredCorrectly) 
    { 
     // Add 50 points to their score, etc. 
    } 
} 
1

あなたは私が書くだろうかプログラムザッツこの

while (score!=50) { 
      // Do stuff 
     } 

のようにwhileループを書くことができ、それ

static void Main(string[] args) { 

     string First; //You should use a char 
     int score = 0; 
     string Second; 


     //TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; 


     Console.Write("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 



     // Here is the while loop 
     // While score is not 50 do stuff 
     while (score!=50) { 
     First = Console.ReadLine(); 
      score = checkanswer(First, "B", 50, score); 
     } 

     Console.Write("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 



     while (score != 100) { Second = Console.ReadLine(); 
      score = checkanswer(Second, "A", 50, score); 
     } 
    } 
    // I added this little fancy function. It makes your program more structured and a little bit smaller ;) 
    static int checkanswer(string userinput, string rightanswer, int winpoints, int score){ 
     if (userinput==rightanswer) { 
      Console.WriteLine("You entered the wrong answer.\n"); 
      Console.WriteLine("Correct!\n" + " score:" + winpoints + score + "\n"); 
      return winpoints + score; 
     } else { 
      Console.WriteLine("You entered the wrong answer.\n"); 
      Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
      return score; 
     } 
    } 
0
bool Ask(string question, string answer, int attempts) 
{ 
    Console.WriteLine(question); 
    for (int i = 0; i < attempts; i++) 
    { 
     string input = Console.ReadLine().ToLower(); 
     if (input == answer) 
     { 
      Console.WriteLine("Correct!"); 
      return true; 
     } 

     Console.WriteLine("Incorrect."); 
    } 

    return false; 
} 

static void Main() 
{ 
    int score = 0; 
    if (Ask("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa", "B", 3)) 
    { 
     score += 50; 
    } 
    else 
    { 
     score -= 50; 
    } 
    Console.WriteLine("Score :" + score); 
} 
0

良いです。 しかし、あなたはちょうどループしながら、学ぶことをしようとしているならば、次は何をしようとする達成するための非常に単純な/愚か/醜い方法です:

Console.Write("Where is the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); 
     var first = ""; 
     bool answeredCorrect = false; //Track B answered. 
     int attempts = 0; //Track 2 attempts. 
     while (!answeredCorrect && attempts < 2) 
     { 
      first = Console.ReadLine(); 
      switch (first) 
      { 

       case "B": 

        Console.WriteLine("You entered the correct answer!"); 
        score = score + 50; 
        Console.WriteLine("Correct!\n" + " score:" + score + "\n"); 

        answeredCorrect = true; 
        break; //break and skip while because "answeredCorrect" is now true. 

       case "A": 

        Console.WriteLine("You entered the wrong answer."); 
        attempts++; //Inkrement attempts. (One more try. Or skip if attemps is 2) 
        break; 

       //case C,D... 

       default: 
        Console.WriteLine("You did not enter a correct answer."); 
        attempts++; //Inkrement attempts. (One more try. Or skip if attemps is 2) 
        break; 
      } 
     } 

     if(first != "B") 
     { 
      Console.WriteLine("Wrong!\n" + " score:" + score + "\n"); 
     } 

(私達の会社のいくつかは、この種のコードを書き込み)

関連する問題