2014-12-15 1 views
0

사용자가 입력 한 텍스트에 각 문자의 출현 목록을 인쇄하려고하는데 텍스트 상자에 마지막 문자 만 표시됩니다. 편지.편지의 번호를 표시하려고하는데 출력에 마지막 문자 만 표시됩니다.

private void button1_Click(object sender, EventArgs e) 
    { 
     //disable the text box so text cannot be entered until reset is pressed 
     textBox3.Enabled = false; 
     //make a list containing the alphabets 
     List<string> Alphabets = new List<string>(); 
     Alphabets.Add("A"); 
     Alphabets.Add("B"); 
     Alphabets.Add("C"); 
     Alphabets.Add("D"); 
     Alphabets.Add("E"); 
     Alphabets.Add("F"); 
     Alphabets.Add("G"); 
     Alphabets.Add("H"); 
     Alphabets.Add("I"); 
     Alphabets.Add("J"); 
     Alphabets.Add("K"); 
     Alphabets.Add("L"); 
     Alphabets.Add("M"); 
     Alphabets.Add("N"); 
     Alphabets.Add("O"); 
     Alphabets.Add("P"); 
     Alphabets.Add("Q"); 
     Alphabets.Add("R"); 
     Alphabets.Add("S"); 
     Alphabets.Add("T"); 
     Alphabets.Add("W"); 
     Alphabets.Add("X"); 
     Alphabets.Add("Y"); 
     Alphabets.Add("Z"); 

     //make a while loop to cycle through alphabets loop 
     int i = 0; 
     while (i < Alphabets.Count) 
     {     
      //assign value in the list Alphabets 
      string SearchFor = Alphabets[i]; 
      //access textbox and make it to upper small small and captital are the same 
      string SearchIn = textBox3.Text.ToUpper(); 
      //list to count the number of instances used for each letter 
      List<int> FoundCount = Search(SearchFor, SearchIn); 
      //if statement so only letters that occor more than 0 times are displayed 
      if (FoundCount.Count > 0) 
      { 
       //string to display the number of occorances 
       //convert to toString so it can be displayed in the TextBox 
       string Counts = Alphabets[i] + ": " + FoundCount.Count.ToString(); 
       //create a message box to display the output 
       //MessageBox.Show(Counts);  
       textBox4.Text = String.Join(Environment.NewLine, Counts);   
      } 
      //adds 1 each time as long as i <alphabet.count 
      i++; 
     } 
    } 
    //List takes 2 arguements (SearchFor, SearchIn) Results can be passed to FoundCounts and number of occrances can be calculted 
    List<int> Search(string Target, string Subject) 
    { 
     //List to count the number of occarances for each letter 
     List<int> Results = new List<int>(); 
     //for loop for comparison (counting occarances) to compare each letter in textbox to each letter in alphabets 
     for (int Index = 0; Index < (Subject.Length - Target.Length) + 1; Index++) 
     { 
      //if to calculate the number of times a letter is used. 
      if (Subject.Substring(Index, Target.Length) == Target) 
      { 
       //adds the number in index to the list Result 
       Results.Add(Index); 
      } 

     } 
     return Results; 
    } 

내가 사실로 여러하지만 didnt 한 작업을했다

+0

여기서 더 쉽게 할 수있는 일이 많이 있습니다. 여러분이 요청한 문제에 관해서는,'while' 루프가 반복 될 때마다'textBox4.Text'의 값을 덮어 쓰기 때문일 수 있습니다. –

+0

문자열 Count 문은 if 문 안에 만 존재하므로 if 문 외부에 textbox4.text를 놓으려고하면 오류가 발생합니다. – sap

+0

'Text' 속성은 문자열 일 뿐이므로 다른 문자열과 마찬가지로 새 값을 연결합니다. 'StringBuilder' 클래스도 연구 할 것을 제안합니다. –

답변

2

난 당신의 코드에 문제가 여기에 확신 : 당신은 때마다 텍스트를 덮어하고

textBox4.Text = String.Join(Environment.NewLine, Counts); 

대신 당신은해야한다 :

textBox4.Text += String.Join(Environment.NewLine, Counts); 
+0

그 덕분에, 고마워! – sap

1

이것은 질문에 대한 직접적인 대답은 아니지만 이것은 OP에 유용 할 수있다.

private void button1_Click(object sender, EventArgs e) 
{ 
    var SearchIn = textBox3.Text.ToUpper(); 

    var query = 
     from c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
     let count = SearchIn.Count(x => x == c) 
     select String.Format("{0}: {1}", c, count); 

    textBox4.Text = String.Join(Environment.NewLine, query);   
} 

가고, 목록을 사용하는 버전 :

이 코드는 (질문에서 제기 된 문제도 수정) 원래의 코드가 한 모든 것을 않습니다. :-)

private void button1_Click(object sender, EventArgs e) 
{ 
    var SearchIn = textBox3.Text.ToUpper(); 
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToList(); 

    var query = 
     from c in alphabet 
     let matches = SearchIn.Where((x, n) => x == c).ToList() 
     select String.Format("{0}: {1}", c, matches.Count()); 

    textBox4.Text = String.Join(Environment.NewLine, query);   
} 
+1

나는 내 대답에 몇 가지 제안을 쓸 자유로운 시간을 찾고 있었는데, 매우 좋은 +1! –

+0

감사합니다. OP 교수는 우리가 그렇게했던 것처럼 목록을 사용하기를 원했습니다. – sap

+0

@sap - 가보 죠. 목록을 사용하는 버전. – Enigmativity

관련 문제