그래, 그래, 숙제를하고있어. 나는 이걸 알고있다. 알아,하지만 한 시간이 넘으면 멍청하게 지내고있다. 내 프로그램을 밖으로 파일을 읽고 당신이 통과 날씨를 말할 루프를 가져옵니다 있지만 구문 오류가 내 foreach 코드를 넣을 경우 목록 상자에 잘못된 대답을 쓸 실 거예요. 이것은 현재 코드입니다.foreach 루프 구문 오류 C# Visual Studio 2013 궁극적 인
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();
}
}
}
가 난 아무데도 여기에 질문을 볼 수 없습니다 .. –
'try '와'catch'블록 사이에'foreach '를 놓을 수 없습니다. try 블록 내부에 있어야합니다. – CodeCaster
오류가 발생했습니다. –