0

私は C# GUI アプリケーションを作成していますが、ループに問題があります。アプリケーションは、学生ドライバー試験の回答 A、B、C、または D を含む .txt ファイルを 2 行に分けて取得し、それを正しい回答と比較しますコード内の配列。私のループは、答えが正しいか間違っているかに関係なく、すべての答えが間違っていることを常に示しています。

私のインターフェース

ここに画像の説明を入力 ここに画像の説明を入力

私のコード:

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) にあると思います。

4

3 に答える 3

2

学生の回答を読んでいるとき、次の行があります。

correctAnswers[index] = sReader.ReadLine();

そのはず:

studentAnswers[index] = sReader.ReadLine();
于 2013-09-28T06:24:19.367 に答える
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);
}
于 2013-09-28T06:27:09.497 に答える
2

一般に、デバッガーを使用して、コードを段階的に実行すると役立ちます。

于 2013-09-28T07:04:21.663 に答える