2016-10-30 25 views
-5

はい、私は宿題をしています。私はそれを知っています。私はそれを知っていますが、iveは1時間以上それをこっそりしています。私はループを取り除いてプログラムを読み込み、渡された天気を言いますが、リストボックスに間違った答えを書くことはありません。もし私が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はあなたの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"); 
} 
+0

これは正解です、ありがとう! – Chris

関連する問題