我正在创建一个 C# GUI 应用程序,但我的循环出现问题,该应用程序获取 .txt 文件,其中包含学生驾驶员考试答案 A、B、C 或 D 的每个分隔行,然后将其与正确答案进行比较代码中的数组。我的循环总是告诉我所有答案都不正确,无论答案是正确还是不正确。
我的界面
我的代码:
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)中非常感谢任何帮助。