2017-09-25 14 views
1

我一直在C#上測試一個測驗程序幾個星期。除了最後一個細節之外,我已經盡其所能地工作了。我希望能夠通過測驗一次,然後在第二次嘗試中使用任何不正確的答案。在兩次嘗試結束時,我都想向用戶顯示評分。任何想法我將如何能夠做到這一點?這裏有一點我的代碼,我正在與:如何在我的測驗中存儲錯誤的答案並在第二次嘗試中重新填充它們

using System; 

namespace Quiz 
{ 
class MultipleChoiceQuiz 
{ 
    public static void CurrentQuestion(string correctAnswer) 
    { 
     do 
     { 
      string userAnswer = Console.ReadLine(); 
      if (userAnswer != "A" && userAnswer != "B" && userAnswer != "C" && userAnswer != "D") 
      { 
       Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input"); 
      } 
      else 
      { 
       if (userAnswer == correctAnswer) 
       { 
        Console.WriteLine("\nThat is correct!"); 
        break; 
       } 
       else if (userAnswer != correctAnswer) 
       { 
        Console.WriteLine("\nSorry, that is incorrect."); 
        break; 
       } 
      } 
     } 
     while (true); 
    } 
    public static void Questions() 
    { 
     Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n"); 
     Console.WriteLine("Please enter your first name: "); 
     string firstName = Console.ReadLine(); 
     Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n"); 
     Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n"); 
     Console.WriteLine("Please submit answers in CAPITAL letter form only.\n"); 
     Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now..."); 
     Console.ReadLine(); 
     Console.Clear(); 

     //Question 1 
     Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n"); 
     Console.WriteLine("Question 1 - What position does the ramp contol knob need to be in? " + 
          "\n\nA. 3N \nB. 1 \nC. 6N \nD. A or C \n\nWhat is your answer " + firstName + "?"); 
     CurrentQuestion("D"); 
     Console.Write("\nPress \'ENTER\' to continue..."); 
     Console.ReadLine(); 
     Console.Clear(); 

     //Question 2 
     Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n"); 
     Console.WriteLine("Question 2 - After power is applied to the aircraft, the battery needs to be turned off? " + 
          "\n\nA. True \nB. False \n\nWhat is your answer " + firstName + "?"); 
     CurrentQuestion("A"); 
     Console.Write("\n"); 
     Console.Write("\nPress \'ENTER\' to continue..."); 
     Console.ReadLine(); 
     Console.Clear(); 

     //Question 3 
     Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n"); 
     Console.WriteLine("Question 3 - Above what temperature does air condition need to be applied to the aircraft while power is applied? " + 
          "\n\nA. 75 degrees Fahrenheit \nB. 100 degrees Fahrenheit \nC. 95 degrees Fahrenheit \nD. 85 degrees Fahrenheit \n\nWhat is your answer " 
          + firstName + "?"); 
     CurrentQuestion("C"); 
     Console.Write("\n"); 
     Console.Write("\nPress \'ENTER\' to continue..."); 
     Console.ReadLine(); 
     Console.Clear(); 

我有一個類和兩個方法。一種方法是將測驗中的答案評估爲對或錯。另一種是向用戶顯示所有問題和答案。任何人可能能夠給我的幫助將不勝感激。

+0

存儲的答案 – UnholySheep

回答

0

這個想法是用適當的結構跟蹤答案。可以使用字典,例如:

Dictionary <string,string> questionsAndAnswers= new Dictionary <string,string>(); 

其中鍵是你的問題,價值就是你的答案。你可以更多地擁有一個保持每個答案正確性的結構。 e.g:

public class Answer 
{ 
    public string text {get;set;} 
    public bool isCorrect {get;set;} 
} 

,並具有:

Dictionary <string,Answer> questionsAndAnswers= new Dictionary <string,Answer>(); 
+0

那是我第一個想到的創建成員變量。不過在第二次看好像球員甚至不能給別的然後A,B,C,d: '如果(userAnswer = 「A」&& userAnswer!=「B」&& userAnswer!=「C」&& userAnswer!=「D」)' – TheSkimek

0

所以,你想再次問的問題是玩家回答不正確的? 爲什麼不把問題編號添加到列表中,然後在第二次嘗試中再次查看該列表。

0

這個問題的面向對象的方法將包括一個代表你的問題的類,以及一個數據結構來保存你的用戶的答案。爲了將答案與問題相關聯,您需要每個問題的標識符。下面的代碼將允許您用許多問題擴展此應用程序,而無需複製/粘貼Console.WriteLine的塊。儘管看起來更快(並且公平我不知道你的動機),但考慮可擴展性和維護性總是一個好主意。一個好的面向對象的結構將允許你做更多的事情,並且編寫更少的代碼。

public class Question 
{ 
    public int Id { get; set; } 

    public string Text { get; set; } 

    public IDictionary<string, string> Answers { get; set; } 

    public string CorrectAnswer { get; set; } 
} 

public static class Program 
{ 
    public static IEnumerable<Question> _questions = new [] 
    { 
     new Question 
     { 
      Id = 1, 
      Text = "What position does the ramp contol knob need to be in?", 
      Answers = new Dictionary<string, string> 
      {, 
       { "A", "3N" }, 
       { "B", "1" }, 
       { "C", "6N" }, 
       { "D", "A or C" } 
      }, 
      CorrectAnswer = "D" 
     } 
    }; 

    public static IDictionary<int, string> _answers = new Dictionary<int, string>(); 

    public static void Main() 
    { 
     Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n"); 
     Console.WriteLine("Please enter your first name: "); 
     string firstName = Console.ReadLine(); 
     Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n"); 
     Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n"); 
     Console.WriteLine("Please submit answers in CAPITAL letter form only.\n"); 
     Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now..."); 
     Console.ReadLine(); 
     Console.Clear(); 

     foreach(var question in _questions) 
     { 
      Console.WriteLine(question.Text); 
      Console.WriteLine(); 
      foreach(var answer in question.Answers) 
      { 
       Console.WriteLine(string.Format("{0}. {1}", answer.Key, answer.Value)); 
      } 
      Console.WriteLine("What is your answer " + firstName + "?"); 
      var response = Console.ReadLine(); 
      while(!question.Answers.ContainsKey(response)) 
      { 
       Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input"); 
       response = Console.ReadLine(); 
      } 
      if(response == question.CorrectAnswer) 
      { 
       Console.WriteLine("\nThat is correct!"); 
      } 
      else 
      { 
       Console.WriteLine("\nSorry, that is incorrect."); 
      } 
      _answers[question.Id] = response; 
     } 

     // do whatever you want when the test is over 
    } 
} 

多一點的解釋:我們創建了一個Question類都有一個標識符,一些文字(實際的問題被問),這是由一個鍵(A,B,C的答案的集合, D等)和一個值(答案的實際文本)以及一個正確的答案。我們打印出一些初始的,獨特的設置說明,其形式是詢問用戶名稱並提供一些關於如何參加測試的提示。

這裏的真正價值來自循環定義良好的對象的能力。我們打印出問題測試,然後回答(格式化以便用戶可以理解並適當選擇),然後詢問用戶答案,確定答案的正確性並存儲答案。

希望這會有所幫助!

相關問題