2016-11-16 74 views
-2

爲什麼我得到INDEXOUTOFBOUNDS EXCEPTION ...請協助並告訴我我哪裏出錯了。我是一個新的程序員。這是代碼;C#..... IndexOutOfBoundsException

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 


namespace ArrayTextFile 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      string filename = "C:\\Users\\Kayloz\\Desktop\\Array.txt"; 
      System.IO.StreamWriter W; 
      W = new System.IO.StreamWriter(filename); 
      int i; 
      string[] names = {"Anthony ","Paul ","Zlatan ","David ","Wayne ","Jose ","Marcus ","Daley","Inez ","Juan "}; 



      for (i = 0; i < names.Length; i++) ; 
      W.Write(names[i]); 
      W.Close(); 



    } 
} 

}

+0

你在哪裏得到的錯誤? – Hank

+5

'for(i = 0; i

+0

'W.Write(names [i]);'不包含在'for'循環中,刪除循環後面的';'它應該可以工作。 – Rabban

回答

0
for (i = 0; i < names.Length; i++) { 
    W.Write(names[i]); 
} 

W.Close(); 
+0

謝謝你們......我把那個; – ryan

2

刪除;for (i = 0; i < names.Length; i++) ;

否則W.Write(names[i]);不重複。 目前你只是重複你的空語句;


備註:既然你不需要i你的循環之外,你應該把它聲明裏面

for (int i = 0; i < names.Length; i++)