2016-10-30 3 views
-5

그래, 그래, 숙제를하고있어. 나는 이걸 알고있다. 알아,하지만 한 시간이 넘으면 멍청하게 지내고있다. 내 프로그램을 밖으로 파일을 읽고 당신이 통과 날씨를 말할 루프를 가져옵니다 있지만 구문 오류가 내 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(); 
     } 
    } 
} 
+0

가 난 아무데도 여기에 질문을 볼 수 없습니다 .. –

+2

'try '와'catch'블록 사이에'foreach '를 놓을 수 없습니다. try 블록 내부에 있어야합니다. – CodeCaster

+1

오류가 발생했습니다. –

답변

0

내가이 약 100 % 확실하지 않다, 그러나 당신의 foreach 문이 시도하고 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"); 
} 
+0

이것은 정확합니다, 대단히 감사합니다 !! – Chris

관련 문제