我已经完成了所有代码,但是需要一些帮助来使转换后的数组正确显示在wordListBox中。任何帮助,将不胜感激。我的代码在下面列出,并且string []一词在类级别中声明。
private void Convert(string[] wordArray, int count)
{
//Convert numbers
for (int index = 0; index < count; index++)
{
if (numbers[index] == 1)
{
wordArray[index] = "one";
}
else if (numbers[index] == 2)
{
wordArray[index] = "two";
}
else if (numbers[index] == 3)
{
wordArray[index] = "three";
}
else if (numbers[index] == 4)
{
wordArray[index] = "four";
}
else if (numbers[index] == 5)
{
wordArray[index] = "five";
}
else if (numbers[index] < 1)
{
wordArray[index] = "lower";
}
else
{
wordListBox.Items.Add("higher");
}
}
}
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
wordListBox.Items.Add(word);
}
答案 0 :(得分:1)
对于变量List<string>
和方法参数string[]
,我将使用word
而不是wordArray
,因此不必初始化数组大小。在ConvertButton_Click
中,您缺少一个foreach
循环,该循环遍历wordArray
中的所有元素并将它们添加到wordListBox
中。这是一个示例:
int[] numbers = { -5, 3, 6, 9, -2, 1, 0, 4};
int count = 8;
List<string> word = new List<string>();
private void Convert(List<string> wordArray, int count)
{
//Convert numbers
for (int index = 0; index < count; index++)
{
if (numbers[index] == 1)
{
wordArray.Add("one");
}
else if (numbers[index] == 2)
{
wordArray.Add("two");
}
else if (numbers[index] == 3)
{
wordArray.Add("three");
}
else if (numbers[index] == 4)
{
wordArray.Add("four");
}
else if (numbers[index] == 5)
{
wordArray.Add("five");
}
else if (numbers[index] < 1)
{
wordArray.Add("lower");
}
else
{
wordArray.Add("higher");
}
}
}
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
foreach (var item in word)
{
wordListBox.Items.Add(item);
}
}
结果:
答案 1 :(得分:0)
尝试:
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
wordListBox.Items.AddRange(word);
}