The background of this program is simple enough: I want a user to be able to enter any letter (A, B, C, etc.) into a textbox
, and with the click of a button, have the program return how many U.S. states begin with that letter (e.g. letter A entered and result is 4).
这个程序的背景很简单:我希望用户能够在文本框中输入任何字母(A,B,C等),只需单击一个按钮,程序就会返回美国的状态数量以该字母开头(例如输入字母A,结果为4)。
Here is my code as of now...
这是我现在的代码......
private void btnClick_Click(object sender, EventArgs e)
{
string[] States = new String[50] {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
"Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
string myLetter = txtboxEnter.Text;
int result;
result = 0;
for (int i = 0; i <= States.Length - 1; i++)
{
if (States[i].Substring(0, 1) == myLetter)
{
result = result + i;
}
else
{
result = 0;
}
}
lblDisplay.Text = Convert.ToString(result);
}
As you see, I have the States declared in an array.
如你所见,我在数组中声明了状态。
The issue I am having is with the for loop and the If
statement inside it.
我遇到的问题是for循环和其中的If语句。
The value always returned is 0
. I feel as though I need another line of code directly for the loop to total values. Am I correct?
总是返回的值是0.我觉得我需要另一行代码直接用于循环到总值。我对么?
4 个解决方案
#1
4
Change this:
改变这个:
if (States[i].Substring(0, 1) == myLetter)
{
result = result + i;
}
else
{
result = 0;
}
To this:
对此:
if (States[i].Substring(0, 1) == myLetter)
{
++result;
}
And if you want to make things a bit more efficient, change that comparison to this:
如果您想提高效率,请将此比较更改为:
if (States[i].StartsWith(myLetter))
#2
3
Linq could be used for a more eloquent solution i.e. use:
Linq可用于更有说服力的解决方案,即使用:
States.Count(state => state.StartsWith(myLetter));
instead of your "for" loop.
而不是你的“for”循环。
#3
0
If all you're doing is finding the number of states that start with each letter, I wouldn't use this kind of solution, since the data is basically static. You could do a table lookup instead using a Dictionary<char, int>
and work with single characters instead of strings, and make it a class variable so you don't have to create it every time. For example:
如果您所做的只是找到以每个字母开头的状态数,我就不会使用这种解决方案,因为数据基本上是静态的。您可以使用Dictionary
//-snip-
private Dictionary<char, int> States;
//-snip-
public Form1()
{
States = new Dictionary<char, int>();
States.add('A', 4);
States.add('B', 0);
States.add('C', 3);
States.add('D', 1);
//... etc.
}
//-snip-
private void btnClick_Click(object sender, EventArgs e)
{
char myLetter = txtboxEnter.Text.Trim()[0]; // do some other input sanitation here
int result = States[myLetter];
lblDisplay.Text = Convert.ToString(result); // consider making your Dictionary <char, string> so you can skip the conversion here
}
And if you still insist on your solution, you can also add some performance tweaks to it by taking advantage of the fact that your array is sorted. You could do a binary search but that's kind of overkill, but at least you could break out of your loop once you have found a state matching the criteria and then a state that no longer matches the criteria (e.g. if you're looking for all words in the English dictionary starting with B, you would stop once you hit a word starting with C).
如果您仍坚持使用您的解决方案,您还可以利用阵列排序的事实为其添加一些性能调整。你可以做一个二元搜索,但这有点矫枉过正,但至少你可以在找到符合条件的状态然后一个不再符合标准的状态时突破你的循环(例如,如果你正在寻找所有在以B开头的英语词典中的单词,一旦你点击以C)开头的单词,你就会停止。
#4
0
Using Lambda
使用Lambda
result = Array.FindAll(States, x => x.StartsWith(myLetter)).Length;
#1
4
Change this:
改变这个:
if (States[i].Substring(0, 1) == myLetter)
{
result = result + i;
}
else
{
result = 0;
}
To this:
对此:
if (States[i].Substring(0, 1) == myLetter)
{
++result;
}
And if you want to make things a bit more efficient, change that comparison to this:
如果您想提高效率,请将此比较更改为:
if (States[i].StartsWith(myLetter))
#2
3
Linq could be used for a more eloquent solution i.e. use:
Linq可用于更有说服力的解决方案,即使用:
States.Count(state => state.StartsWith(myLetter));
instead of your "for" loop.
而不是你的“for”循环。
#3
0
If all you're doing is finding the number of states that start with each letter, I wouldn't use this kind of solution, since the data is basically static. You could do a table lookup instead using a Dictionary<char, int>
and work with single characters instead of strings, and make it a class variable so you don't have to create it every time. For example:
如果您所做的只是找到以每个字母开头的状态数,我就不会使用这种解决方案,因为数据基本上是静态的。您可以使用Dictionary
//-snip-
private Dictionary<char, int> States;
//-snip-
public Form1()
{
States = new Dictionary<char, int>();
States.add('A', 4);
States.add('B', 0);
States.add('C', 3);
States.add('D', 1);
//... etc.
}
//-snip-
private void btnClick_Click(object sender, EventArgs e)
{
char myLetter = txtboxEnter.Text.Trim()[0]; // do some other input sanitation here
int result = States[myLetter];
lblDisplay.Text = Convert.ToString(result); // consider making your Dictionary <char, string> so you can skip the conversion here
}
And if you still insist on your solution, you can also add some performance tweaks to it by taking advantage of the fact that your array is sorted. You could do a binary search but that's kind of overkill, but at least you could break out of your loop once you have found a state matching the criteria and then a state that no longer matches the criteria (e.g. if you're looking for all words in the English dictionary starting with B, you would stop once you hit a word starting with C).
如果您仍坚持使用您的解决方案,您还可以利用阵列排序的事实为其添加一些性能调整。你可以做一个二元搜索,但这有点矫枉过正,但至少你可以在找到符合条件的状态然后一个不再符合标准的状态时突破你的循环(例如,如果你正在寻找所有在以B开头的英语词典中的单词,一旦你点击以C)开头的单词,你就会停止。
#4
0
Using Lambda
使用Lambda
result = Array.FindAll(States, x => x.StartsWith(myLetter)).Length;