C# 我试图显示一个字母的occarance数,但输出仅显示最后一个字母
我正在尝试打印用户输入的文本中每个字母出现的列表,但文本框仅显示最后一个字母C# 我试图显示一个字母的occarance数,但输出仅显示最后一个字母,c#,user-interface,C#,User Interface,我正在尝试打印用户输入的文本中每个字母出现的列表,但文本框仅显示最后一个字母 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 t
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;
}
private void按钮1\u单击(对象发送者,事件参数e)
{
//禁用文本框,以便在按下重置之前无法输入文本
textBox3.Enabled=false;
//列一张包含字母表的清单
列表字母=新列表();
字母表。添加(“A”);
字母表。添加(“B”);
字母表。添加(“C”);
字母表。添加(“D”);
字母。添加(“E”);
字母表。添加(“F”);
字母表。添加(“G”);
字母。添加(“H”);
字母。添加(“I”);
字母表。添加(“J”);
字母。添加(“K”);
字母。添加(“L”);
字母表。添加(“M”);
字母表。添加(“N”);
字母。添加(“O”);
字母。添加(“P”);
字母。添加(“Q”);
字母。添加(“R”);
字母表。添加(“S”);
字母表。添加(“T”);
字母。添加(“W”);
字母表。添加(“X”);
字母表。添加(“Y”);
字母表。添加(“Z”);
//做一个while循环,在字母循环中循环
int i=0;
而(i0)
{
//字符串以显示occorance的数量
//转换为toString,以便在文本框中显示
字符串计数=字母表[i]+:“+FoundCount.Count.ToString();
//创建一个消息框以显示输出
//MessageBox.Show(计数);
textBox4.Text=String.Join(Environment.NewLine,Counts);
}
//每次添加1,只要我我很确定代码的问题在这里:
textBox4.Text = String.Join(Environment.NewLine, Counts);
您每次都在覆盖文本,您应该执行以下操作:
textBox4.Text += String.Join(Environment.NewLine, Counts);
我敢肯定,您的代码存在以下问题:
textBox4.Text = String.Join(Environment.NewLine, Counts);
您每次都在覆盖文本,您应该执行以下操作:
textBox4.Text += String.Join(Environment.NewLine, Counts);
我敢肯定,您的代码存在以下问题:
textBox4.Text = String.Join(Environment.NewLine, Counts);
您每次都在覆盖文本,您应该执行以下操作:
textBox4.Text += String.Join(Environment.NewLine, Counts);
我敢肯定,您的代码存在以下问题:
textBox4.Text = String.Join(Environment.NewLine, Counts);
您每次都在覆盖文本,您应该执行以下操作:
textBox4.Text += String.Join(Environment.NewLine, Counts);
这不是对这个问题的直接回答,但这可能对OP很有价值
此代码完成了原始代码所做的一切(并修复了问题中提出的问题):
这是一个使用列表的版本。:-)
这不是对这个问题的直接回答,但这可能对OP很有价值
此代码完成了原始代码所做的一切(并修复了问题中提出的问题):
这是一个使用列表的版本。:-)
这不是对这个问题的直接回答,但这可能对OP很有价值
此代码完成了原始代码所做的一切(并修复了问题中提出的问题):
这是一个使用列表的版本。:-)
这不是对这个问题的直接回答,但这可能对OP很有价值
此代码完成了原始代码所做的一切(并修复了问题中提出的问题):
这是一个使用列表的版本。:-)
字符串计数仅存在于if语句内部,因此当我尝试将textbox4.text置于if语句外部时,它会触发错误。字符串计数仅存在于if语句内部,因此当我尝试将textbox4.text置于if语句外部时,它会触发错误。字符串计数仅存在于if语句内部,因此当我尝试将textbo置于if语句外部时x4.text在if语句外,它会引发错误。字符串计数只存在于if语句内,因此当我尝试将textbox4.text放在if语句外时,它会引发错误。我正在寻找一些空闲时间在我的答案中写一些建议,非常好+1!谢谢。OP教授希望我们使用列表,所以我用了我的方法。@sap-好了。这是一个使用列表的版本。我想找一些空闲时间在我的答案中写一些建议,非常好+1!谢谢。OP教授希望我们使用列表,所以我就这样做了。@sap-好了。这是一个使用列表的版本。我想找一些空闲时间在我的答案中写一些建议,非常好+1!谢谢。OP教授r希望我们使用列表,所以我照着我的方式做了。@sap-好了。这是一个使用列表的版本。我正在寻找一些空闲时间在我的答案中写一些建议,非常好+1!谢谢。OP教授希望我们使用列表,所以我照着我的方式做了。@sap-好了。这是一个使用列表的版本。