我正在尝试打印用户输入的每个字母的出现列表,但文本框仅显示最后一个字母。
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;
}
我将多线作为真,但是没有效果
答案 0 :(得分:2)
我很确定您的代码存在问题:
textBox4.Text = String.Join(Environment.NewLine, Counts);
您每次都会覆盖文本,而应该这样做:
textBox4.Text += String.Join(Environment.NewLine, Counts);
答案 1 :(得分: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);
}