我正在創建一個C#GUI應用程序,我遇到了問題,我的循環中,應用程序獲取帶有學生驅動程序考試答案的A,B,C或D的.txt文件然後將它與代碼中數組中的正確答案進行比較。無論答案是正確還是不正確,我的循環總是會給我所有答案都不正確。C#中的循環問題比較兩個數組
我的界面
我的代碼:
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) 任何幫助深表感謝。
文件中的字符大小寫是什麼?他們是大寫的「A,B,C ...」還是小寫的「a,b,c」? – Alireza
嗨Alireza,我也使用大寫,謝謝 – user2318228