2009-09-15 42 views
0

我創建了一個應用程序,該應用程序使用基於單詞計數結果的值填充30個文本框。基於30個用戶創建一個循環?

最終版本將有30個字,但這個測試AP我只包括其中的3

如何WOLD你去使這在一個循環?

int[] totX = new int[30]; 


    string nav1 = "test1"; 
    string nav2 = "test2"; 
    string nav3 = "test3"; 



    public Form1() 
    { 

     using (StreamReader sr = new StreamReader(@"c:\temp\output.txt")) 
     { 
      var total = 0; 

      while (!sr.EndOfStream) 
      { 
       var counts = sr 
        .ReadLine() 
        .Split('"') 
        .GroupBy(s => s) 
        .Select(g => new { Word = g.Key, Count = g.Count() }); 
       var wc = counts.SingleOrDefault(c => c.Word == nav1); 
       total += (wc == null) ? 0 : wc.Count; 
       totX[0] = total; 
      } 

     } 
     using (StreamReader sr = new StreamReader(@"c:\temp\output.txt")) 
     { 
      var total = 0; 

      while (!sr.EndOfStream) 
      { 
       var counts = sr 
        .ReadLine() 
        .Split('"') 
        .GroupBy(s => s) 
        .Select(g => new { Word = g.Key, Count = g.Count() }); 
       var wc = counts.SingleOrDefault(c => c.Word == nav2); 
       total += (wc == null) ? 0 : wc.Count; 
       totX[1] = total; 
      } 

     } 
     using (StreamReader sr = new StreamReader(@"c:\temp\output.txt")) 
     { 
      var total = 0; 

      while (!sr.EndOfStream) 
      { 
       var counts = sr 
        .ReadLine() 
        .Split('"') 
        .GroupBy(s => s) 
        .Select(g => new { Word = g.Key, Count = g.Count() }); 
       var wc = counts.SingleOrDefault(c => c.Word == nav3); 
       total += (wc == null) ? 0 : wc.Count; 
       totX[2] = total; 
      } 

     } 

     InitializeComponent(); 


    } 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     textBox1.Text = totX[0].ToString(); 
     textBox2.Text = totX[1].ToString(); 
     textBox3.Text = totX[2].ToString(); 

    } 


} 

}

+0

什麼類型的文件是嗎?內容是什麼樣的? – shahkalpesh 2009-09-15 17:19:19

+0

這是一個很長的日誌文件,,,繼續隱含的字符串與「分離真正的單詞。後面的單詞是字符串中的用戶名。 – Darkmage 2009-09-15 18:23:34

回答

2

做一個普通的for循環在你的流,並使用計數器作爲索引你TOTX陣列英寸導航字符串也可以在每次迭代中創建。

此外,實際上不需要讀取同一個文件30次,所以我建議您只讀一次文件,然後根據需要檢查內容。

0

THX布萊恩得到這個工作

int[] totX = new int[3]; 
    string[] navX = new string[] {"test1", "test2", "test3"}; 


    public Form1() 
    { 

     for (int i = 0; i < 3; i++) 
     { 
      using (StreamReader sr = new StreamReader(@"c:\temp\output.txt")) 
      { 

       var total = 0; 

       while (!sr.EndOfStream) 
       { 
        var counts = sr 
         .ReadLine() 
         .Split('"') 
         .GroupBy(s => s) 
         .Select(g => new { Word = g.Key, Count = g.Count() }); 
        var wc = counts.SingleOrDefault(c => c.Word == navX[i]); 
        total += (wc == null) ? 0 : wc.Count; 
        totX[i] = total; 
       } 

      } 
     } 

     InitializeComponent(); 


    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     textBox1.Text = totX[0].ToString(); 
     textBox2.Text = totX[1].ToString(); 
     textBox3.Text = totX[2].ToString(); 
    } 

}