2013-09-28 80 views
0

我正在創建一個C#GUI應用程序,我遇到了問題,我的循環中,應用程序獲取帶有學生驅動程序考試答案的A,B,C或D的.txt文件然後將它與代碼中數組中的正確答案進行比較。無論答案是正確還是不正確,我的循環總是會給我所有答案都不正確。C#中的循環問題比較兩個數組

我的界面

enter image description hereenter image description here

我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace Mohammad_Saad_Assignment_1_Part_2_Sec_B 
{ 
public partial class Form1 : Form 
{ 
    //Acutal answers 
    string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", 
      "C", "D", "A", "D", "C", "C", "B", "D", "A" }; 

    //Student answers array of size 20 
    string[] studentAnswers = new string[20]; 

    public Form1() 
    { 

     InitializeComponent(); 
    } 

    private void btnOpen_Click(object sender, EventArgs e) 
    { 
     listBox2.Items.AddRange(correctAnswers); 

     //Opening a new file. 
     OpenFileDialog open = new OpenFileDialog(); 
     //Dismiss user cancelation so application dosnt crash. 
     if (open.ShowDialog() == DialogResult.OK) 
     { 
      path.Text = open.FileName; 
     } 

     StreamReader sReader = new StreamReader(open.FileName); 
     int index = 0; 

     while (index < correctAnswers.Length && !sReader.EndOfStream) 
     { 
      correctAnswers[index] = sReader.ReadLine(); 
      index++; 
     } 
     foreach (string str in correctAnswers) 
     { 
      listBox1.Items.Add(str); 
     } 

     btnOpen.Enabled = false; 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     int i = 0; 
     int questionNumber = 1; 
     int answersCorrect = 0; 
     int answersIncorrect = 0; 
     do 
     { 
      if (studentAnswers[i] == correctAnswers[i]) 
      { 
       answersCorrect++; 
      } 
      if (studentAnswers[i] != correctAnswers[i]) 
      { 
       listBox3.Items.Add("Question " + questionNumber); 
       listBox3.Items.Add("Incorrect"); 
       answersIncorrect++; 
      } 
      i++; 
      questionNumber++; 
     } 

     while (i != 20); 
     lblCorrect.Text = answersCorrect.ToString(); 
     lblInCorrect.Text = answersIncorrect.ToString(); 
     if (answersCorrect >= 15) 
     { 
      txtResults.Text = "PASS"; 

     } 
     if (answersCorrect < 15) 
     { 
      txtResults.Text = "FAIL"; 

     } 

     btnMark.Enabled = false; 
    } 

    private void btnClear_Click(object sender, EventArgs e) 
    { 
     btnOpen.Enabled = true; 
     btnMark.Enabled = true; 
     listBox1.Items.Clear(); 
     listBox3.Items.Clear(); 
     txtResults.Clear(); 
    } 
} 
} 

的問題是,我認爲是在標記按鈕(按鈕1) 任何幫助深表感謝。

+0

文件中的字符大小寫是什麼?他們是大寫的「A,B,C ...」還是小寫的「a,b,c」? – Alireza

+0

嗨Alireza,我也使用大寫,謝謝 – user2318228

回答

2

更改此代碼塊:

while (index < correctAnswers.Length && !sReader.EndOfStream) 
{ 
    correctAnswers[index] = sReader.ReadLine(); 
    index++; 
} 
foreach (string str in correctAnswers) 
{ 
    listBox1.Items.Add(str); 
} 

到:

while (index < correctAnswers.Length && !sReader.EndOfStream) 
{ 
    studentAnswers[index] = sReader.ReadLine(); 
    index++; 
} 
foreach (string str in studentAnswers) 
{ 
    listBox1.Items.Add(str); 
} 
2

你有這條線,當你在學生的答案閱讀:

correctAnswers[index] = sReader.ReadLine(); 

它應該是:

studentAnswers[index] = sReader.ReadLine(); 
+0

正是。然後在兩個列表框中顯示相同的數組。但是當他檢查結果時,他比較了數組{{null,null,null,...}到'{「B」,「D」,「A」,...}。一個無關的建議是驗證文件內容。像文件「A」(帶有空格)的字符串可能會導致更細微的錯誤。 –

2

通常,使用調試器並逐步執行代碼將對您有所幫助。