foreach循环语法错误c# Visual Studio 2013终极版

本文关键字:Studio 2013 终极版 Visual 循环 语法 错误 foreach | 更新日期: 2023-09-27 18:15:38

好的,所以是的,我正在做作业,我非常接近这个,我知道它,但我已经弄乱了一个多小时,现在我要疯了,如果我把循环取出我的程序将读取文件并说你通过了,但它不会在列表框中写出错误的答案,如果我把我的foreach代码给我一个语法错误。这是我当前的代码。

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;
using System.IO;
namespace DriversLicenseExam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string[] answerArray ={"B","D","A","A","C",
                                  "A", "B","A","C","D",
                                  "B", "C","D","A","D",
                                  "C", "C","B","D","A"};
            string[] studentansArray = new string[20];
            List<string> incorrectList = new List<string>();
            int count = 0, index = 0, qnumber = 0;
            try
            {
                string filename = "../../" + filenametxt.Text;
                StreamReader inputFile = File.OpenText(filename);
                while(!inputFile.EndOfStream)
                {
                    studentansArray[index] = inputFile.ReadLine();
                    if (studentansArray[index] == answerArray[index])
                        count++;
                    else
                    {
                        qnumber = index + 1;
                            incorrectList.Add(qnumber.ToString());
                    }
                    index++;
                }
                inputFile.Close();
                if (count >= 15)
                {
                    resultoutput.Text = "You Passed The Test!";
                }
                else
                    resultoutput.Text = "You Failed The Test... You're a Failure!";
            }
            foreach (string str in incorrectList)  // <<-- error is here
            {
                lbox.Items.Add(str);
            }                                      // <<-- error is here
            catch (Exception)
            {
                MessageBox.Show("File Not Found");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            filenametxt.Text = "";
            resultoutput.Text = "";
            lbox.Items.Clear();
        }
        private void exitbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

foreach循环语法错误c# Visual Studio 2013终极版

我不是100%确定这一点,但你的foreach是在你的try和catch块之间,也许试试那个:

try
{
    string filename = "../../" + filenametxt.Text;
    StreamReader inputFile = File.OpenText(filename);
    while(!inputFile.EndOfStream)
    {
        studentansArray[index] = inputFile.ReadLine();
        if (studentansArray[index] == answerArray[index])
            count++;
        else
        {
            qnumber = index + 1;
            incorrectList.Add(qnumber.ToString());
        }
        index++;
    }
    inputFile.Close();
    if (count >= 15)
    {
        resultoutput.Text = "You Passed The Test!";
    }
    else
        resultoutput.Text = "You Failed The Test... You're a Failure!";
    foreach (string str in incorrectList)
    {
        lbox.Items.Add(str);
    }
}
catch (Exception)
{
    MessageBox.Show("File Not Found");
}