2016-12-01 49 views
0

我的任務如下。C#程序不能正確顯示輸出

某ITEC考試有20個選擇題。下面是正確的答案:

1. B 11. B 
2. D 12. C 
3. A 13. D 
4. A 14. A 
5. C 15. D 
6. A 16. C 
7. B 17. C 
8. B 18. B 
9. C 19. D 
10. D 20. A 

創建的程序:

  1. 創建存儲了正確的答案(5分)的陣列。
  2. 創建一個名爲Response.txt的文本文件,它是模擬回答問題的學生 只寫出字母答案,每行只寫一個字母。記住你的文件必須 有20行從A,B,C或D.答案見下例 一個 ç 乙 d Ç
  3. 你的程序應該在一個數組
  4. 閱讀文本文件和存儲的答案
  5. 學生的答案從文件中讀取並存儲在數組中後,程序應該 將數組與正確的答案進行比較,數組與答案的答案和 確定有多少答案是正確的,有多少錯誤。
  6. 程序應顯示一條消息,指出學生是否通過了 考試。通過分數是14正確的答案。
  7. 程序應顯示正確答案的總數,錯誤答案的總數以及顯示錯誤回答問題的問題編號的列表。

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    
    namespace ITEC_exam 
    { 
        public partial class ITEC_exam : Form 
        { 
         public ITEC_exam() 
         { 
          InitializeComponent(); 
         } 
    
         private void button1_Click(object sender, EventArgs e) 
         { 
    
          string line; 
          int cnt = 0; 
    
          //List to hold question numbers of incorrect answers 
          List<int> incorrect = new List<int>(); 
    
          //Array to store correct answers 
          string[] correctAnswers = { }; 
    
          //Array to store answers 
          string[] answers = { }; 
    
          //Read the files and store answers in arrays 
          System.IO.StreamReader correctFile = new System.IO.StreamReader("C:\\Users\\a_day\\Desktop\\Baker_Austin_c#_final\\ITECexam\\ITEC exam\\correctAnswers.txt"); 
          System.IO.StreamReader answerFile = new System.IO.StreamReader("C:\\Users\\a_day\\Desktop\\Baker_Austin_c#_final\\ITECexam\\ITEC exam\\testResult.txt"); 
    
          if ((line = correctFile.ReadLine()) != null) 
           correctAnswers = line.Split(' '); 
    
          if ((line = answerFile.ReadLine()) != null) 
           answers = line.Split(' '); 
    
          //Compare answers and compute the score 
          for (int i = 0; i < 20; i++) 
          { 
           if (correctAnswers.Count() > i && answers.Count() > i) 
           { 
            if (String.Compare(correctAnswers[i], answers[i]) == 0) 
             cnt++; 
            else 
             incorrect.Add(i + 1); 
           } 
          } 
    
          //Print Result 
          if (cnt >= 15) 
           MessageBox.Show("\n\n Result: PASS"); 
          else 
           MessageBox.Show("\n\n Result: FAIL"); 
    
          //Printing score 
          MessageBox.Show("\n Total number of Correct Answers: " + cnt); 
          MessageBox.Show("\n Total number of Incorrect Answers: " + (20 - cnt)); 
    
          MessageBox.Show("\n Question numbers of incorrect answers: "); 
          //Printing incorrectly answered question numbers 
          foreach (int qno in incorrect) 
           MessageBox.Show(" " + qno + " "); 
    
          //Closing Files 
          correctFile.Close(); 
          answerFile.Close(); 
    
    
         } 
        } 
    } 
    

我遇到的問題是,我似乎無法得到它的輸出,顯示的不正確回答問題,問題編號列表。

編輯:我遇到的問題是,我已確保答案的關鍵和響應文件都有相同的信息,所以程序應該導致與通過,並沒有不正確的答案。

發生了什麼事情是該程序說只有1個正確的答案,那麼它也沒有列出它的sttd不正確的答案。

+0

你重新廣告只有一行文件。你的文件如何看起來像?所有的信息都寫在一行嗎?這種情況背後的原因是:if(correctAnswers.Count()> i && answers.Count()> i)'。你有沒有試過一步一步通過調試器? –

+0

乙 d 甲 甲 Ç 甲 乙 乙 Ç d 乙 Ç d 甲 d Ç Ç 乙 d 甲 – Austin

+0

HMM在記事本中它是垂直的,但是當我複製並粘貼它的輸出,如 – Austin

回答

0

你可以使用一個StringBuilder(或只是連接字符串)是這樣的:

  //Printing score 
      MessageBox.Show("\n Total number of Correct Answers: " + cnt); 
      MessageBox.Show("\n Total number of Incorrect Answers: " + (20 - cnt)); 

      var sb = new StringBuilder(); 
      foreach (int qno in incorrect) 
       sb.Append(" " + qno + " "); 

      MessageBox.Show("\n Question numbers of incorrect answers: " + sb.ToString()); 
      //Printing incorrectly answered question numbers 
+0

主要問題是由於某種原因,它會說只有1是正確的(我已確保答案文件和響應文件匹配),它確實不列出任何它的sttd不正確的答案 – Austin

0

不幸遇到讀只有一個從文件一行。 只有在文件中有1行時,您的代碼才能正常工作。

如果您的文件中有數據列/垂直對齊,我會建議使用File.ReadAllLines()。它將返回所有行的string[]

string[] correctAnswers = File.ReadAllLines("filepath"); 
string[] answers = File.ReadAllLines("filepath"); 

的另一件事是你的條件if (correctAnswers.Count() > i && answers.Count() > i)

你不需要它,如果在這兩個文件的順序是一樣的。你可以簡單地比較一下。

for循環應運行,直到您試圖索引的數組的末尾。如果在下一次考試中您將有24個問題,這個循環將跳出界限。因此,一個更安全的替代方法是使用Length屬性或數組:

//Compare answers and compute the score 
for (int i = 0; i < answers .Lengt; i++) 
{ 

編輯:

如果你有文件之間的不匹配,則比較可能是危險的,所以你可以檢查是否兩個陣列具有相同的長度:

if(answers.Lenngth == correctAnswers.Length) 

,然後才執行循環和比較

+0

謝謝!我把if(correctAnswers.Count()> i && answers.Count()> i)出來,我添加它的原因似乎是由於我的txt文件不匹配造成的 – Austin

+0

如果你有一個不匹配的比較可能是危險的。檢查我的編輯 –