【问题标题】:Need to sort words in an array using a separate method需要使用单独的方法对数组中的单词进行排序
【发布时间】:2018-03-15 02:01:41
【问题描述】:

问题: 编写一个名为 SortWords 的程序,其中包含一个接受任意数量单词并按字母顺序对它们进行排序的方法。证明当用一个、两个、五个或十个单词调用方法时程序可以正常工作。

到目前为止我所拥有的:

private void button1_Click(object sender, EventArgs e)
{
    String[] outWords = new string[20];       
    outWords[0] = textbox1.Text;
    outWords[1] = textBox2.Text;
    outWords[2] = textBox3.Text;
    outWords[3] = textBox4.Text;
    outWords[4] = textBox5.Text;
    outWords[5] = textBox6.Text;
    outWords[6] = textBox7.Text;
    outWords[7] = textBox8.Text;
    outWords[8] = textBox9.Text;
    outWords[9] = textBox10.Text;

    sortAndPrint(outWords[11]);
}

private void sortAndPrint(params string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
    label6.Text = newWords[5];
    label7.Text = newWords[6];
    label8.Text = newWords[7];
    label9.Text = newWords[8];
    label10.Text = newWords[9];
}

我的问题是我的标签框中没有任何内容,或者由于无法将字符串转换为string[]System.IndexOutOfRangeException 而引发错误。我确定我在这里做错了什么。

【问题讨论】:

  • 您声明sortAndPrint(outWords[11]);outWords[11] 是什么?它似乎从未被分配。也许您应该传递整个数组 outWords,或者按照赋值状态,将 1、2、5 和 10 个单词传递给方法。
  • 您的方法接受任意数量的单词,但您是在newWords params 数组中可能存在也可能不存在的硬编码索引。您不应该假设这一点,而是使用循环来输出值。确保您了解 params string[] 和普通的旧 string[] 之间的区别。
  • 检查下面的答案。你的代码有很多变化。您可以轻松识别。

标签: c# arrays sorting


【解决方案1】:

试试这个:

private void button1_Click(object sender, EventArgs e)
{
    string[] outWords = new string[10];

    outWords[0] = textbox1.Text;
    outWords[1] = textBox2.Text;
    outWords[2] = textBox3.Text;
    outWords[3] = textBox4.Text;
    outWords[4] = textBox5.Text;
    outWords[5] = textBox6.Text;
    outWords[6] = textBox7.Text;
    outWords[7] = textBox8.Text;
    outWords[8] = textBox9.Text;
    outWords[9] = textBox10.Text;

    sortAndPrint(outWords);

}

private void sortAndPrint(string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
    label6.Text = newWords[5];
    label7.Text = newWords[6];
    label8.Text = newWords[7];
    label9.Text = newWords[8];
    label10.Text = newWords[9];
}

总结

传递整个数组 sortAndPrint(outWords); 而不是单个元素。

只取你需要的数组长度。 string[] outWords = new string[10];

请查看this question 以了解params 的使用情况。使用 param 时需要传递值,但如果需要传递变量,则必须删除 params

参数示例

private void button1_Click(object sender, EventArgs e)
{
    sortAndPrint("one","two","three","four","five");
}

private void sortAndPrint(params string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
}

【讨论】:

  • “证明当方法被一个、两个、五个或十个单词调用时程序正常工作”我不认为传入整个数组是这里的实际目标......(因此签名是params string[],而不是string[]
  • @RufusL 在总结中又补充了一点。感谢您注意到这一点。 :)
【解决方案2】:
sortAndPrint(outWords[11]);

会将单个字符串(作为数组)传递给sortAndPrint,所以当你调用时

label2.Text = newWords[1];

你得到一个越界异常。试试吧

sortAndPrint(outWords);

传递整个数组。另请注意,数组中的空槽将在 其他字符串之前排序,因此您需要想出一种方法来摆脱空白/空字符串。

或者,如果目的是演示如何使用 params,您可以执行以下操作:

sortAndPrint(textbox1.Text, 
             textbox2.Text,  
             textbox3.Text,  
             textbox4.Text,  
             textbox5.Text);

但是你需要检查sortAndPrint中数组的边界,而不是仅仅假设数组的大小至少为10。

【讨论】:

    【解决方案3】:

    如果你仔细阅读说明,你有这个要求:

    ...包括一个接受任意数量单词的方法...

    您可以通过使用 params 关键字和 string[] 来完成此操作。

    ...并按字母顺序对它们进行排序。

    这部分你用Array.Sort(newWords);

    证明当用一、二、五或十个单词调用方法时程序正常工作

    这部分你没有做 - 你在你的代码中假设输入数组将有 10 个项目,而你应该在输出结果之前检查它有多少项目。

    由于该方法无法确定数组大小,因此无法假设表单上有足够的标签来填充结果。鉴于此,我们可以使用MessageBox 来显示结果,我们可以使用String.Join 将数组中的所有项目与Environment.NewLine 字符连接起来,以便在不同的行中显示排序的单词:

    private void SortAndPrint(params string[] newWords)
    {
        Array.Sort(newWords);
        MessageBox.Show(string.Join(Environment.NewLine, newWords));
    }
    

    现在,我们可以通过向它传递不同数量的参数来演示该函数的用法。

    首先,我们在同一个页面上,我在Form_Load 方法中有这段代码,它向表单添加了 10 个文本框,所有文本框都带有“输入”标签:

    private void Form1_Load(object sender, EventArgs e)
    {
        var stdHeight = 20;
        var stdWidth = 100;
        var stdPad = 10;
        var count = 10;
    
        for (int i = 0; i < count; i++)
        {
            var textBox = new TextBox
            {
                Name = "textBox" + (i + 1),
                Left = stdPad,
                Width = stdWidth,
                Height = stdHeight,
                Top = (stdHeight + stdPad) * i + stdPad,
                Tag = "input"
            };
            Controls.Add(textBox);
        }
    }
    

    现在,在我们的按钮单击事件中,我们可以搜索表单上具有Tag == "input".Text 属性不为空的所有控件,我们可以将这些Text 值传递给我们的方法单个数组作为单个项目:

    private void button1_Click(object sender, EventArgs e)
    {
        // Select all textbox Text fields that have some value
        var words = Controls.Cast<Control>()
            .Where(t => t.Tag == "input" && !string.IsNullOrWhiteSpace(t.Text))
            .Select(t => t.Text)
            .ToArray();
    
        // Pass all the words in a single array to our method
        SortAndPrint(words);
    
        // We can also demonstrate this by passing individual words
        // Pass one word if we can
        if (words.Length > 0)
        {
            SortAndPrint(words[0]);
        }
        // Pass two words if we can
        if (words.Length > 1)
        {
            SortAndPrint(words[0], words[1]);
        }
        // Pass five words if we can
        if (words.Length > 4)
        {
            SortAndPrint(words[0], words[1], words[2], words[3], words[4]);
        }
        // Pass ten words if we can
        if (words.Length > 9)
        {
            SortAndPrint(words[0], words[1], words[2], words[3], words[4], 
                            words[5], words[6], words[7], words[8], words[9]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多