【问题标题】:How to check if strings in textboxes are in ascending order如何检查文本框中的字符串是否按升序排列
【发布时间】:2021-09-30 23:29:02
【问题描述】:

我有一个生成 10 个随机字符串的应用程序,然后我要求用户按升序将这 10 个字符串输入到 10 个文本框中,一旦完成,用户单击一个按钮检查顺序是否正确,然后给他们一条消息是否正确。

问题:当用户将我的列表框中生成的每个值输入到 10 个 10 个文本框中,无论顺序如何,然后单击复选按钮,就会显示成功消息。这不应该发生,因为用户只输入了列表框中的值并且没有按升序输入它们。但是,如果用户输入了不在列表框中的错误值,则会显示失败消息。如果顺序不是升序,则也应显示失败消息。

The first image shows the strings being placed in ascending order and a success message being shown,Note that the only reason the success message is been showing is because the user entered all the values in the textbox that are present in the list box and not because they are in ascending order

In the second image if you look at the first two textboxes you will see that the first value and second value have been swapped.This means the values entered are no longer in ascending order and therefore the failure message should be shown however the application is not checking if the values are sorted correctly and is still displaying the success message

The last picture shows that if a value which doesn't exist in the list box is entered in any of the texboxes,a failure message will display.This supposed to happen however the failure message should also appear if the order was not in ascending

   public static List<string> values = new List<string> { "001.45 EBC", "004.56 VWH", "002.44 MFH", "003.88 CSK", "006.96 FEB", "008.77 NJC", "005.23 MKF", "007.62 IJN", "010.13 FNH", "009.88 ENC" };//list of call numbers created
    public ReplaceBooksPage() //replace  books class
    {
        InitializeComponent();
    }

    private void generate_Click(object sender, EventArgs e) //generate button created
    {


     listBox1.Items.Clear();//clears items in the list box

    var random = new Random(); //random class created

    for (var i = 0; i < 10; i++) {//for loop created that adds 10 random numbers to listbox
 
     int index = random.Next(values.Count); 
    listBox1.Items.Add(values[index]);//adds items to listbox

}

    }
   private void btn_sort(object sender, EventArgs e)//generate button created
    {
          List<string> list2 = new List<string>();//list created
    var list = listBox1.Items.Cast<string>().ToList();
        list.Sort();//sorts he list

        list2.Add(textBox1.Text); //adds each textbox to the list
        list2.Add(textBox2.Text);
        list2.Add(textBox3.Text);
        list2.Add(textBox4.Text);
        list2.Add(textBox5.Text);
        list2.Add(textBox6.Text);
        list2.Add(textBox7.Text);
        list2.Add(textBox8.Text);
        list2.Add(textBox9.Text);
        list2.Add(textBox10.Text);
        list2.Sort();
        if (list.SequenceEqual(list2))//if statment created
        {
            MessageBox.Show("sucess"); //disaplays a message to the user if the ordering was correct
        }
        else //else statement
        {
            MessageBox.Show("fail"); //displays a message to the user if the ordering was incorrect
        }
    } 

【问题讨论】:

  • list2.Sort():这会对输入的值进行排序,因此您可以比较两个已经排序的列表。我认为您想删除此行。
  • 是的,但是无论我输入什么顺序或值,当我删除 list2.sort() 时它总是会失败
  • 这是 SO 上 2/3 天内关于同一主题的第三个问题!!

标签: c# string winforms


【解决方案1】:

您正在对按钮单击时输入的用户列表进行排序,因此它会将其排序为正确的顺序,然后当他们输入一个不存在的选项时,它不会匹配,因此它不会返回正确。不排序list2

【讨论】:

  • 是的,但是当我不对 list2 进行排序时,当我输入任何值时它总是会失败
【解决方案2】:

正如上面所说的:如果您希望它按预期工作,则必须删除 list2.Sort()。如果仅删除该排序导致比较结果中出现False,则输入的字符串相等。请看下面的小例子:

        var list1 = new string[] { "3 three", "1 one", "2 two" }.ToList();
        var list2 = new string[] { "1 one", "2 two", "3 three" }.ToList();
        
        foreach (var item in list1) {
            Console.WriteLine(item);
        }
        
        Console.WriteLine($"Are lists equal: {list1.SequenceEqual(list2)}");
        Console.WriteLine();
        
        list1.Sort();
        
        foreach (var item in list1) {
            Console.WriteLine(item);
        }
        
        Console.WriteLine($"Are lists equal: {list1.SequenceEqual(list2)}");
        Console.WriteLine();

您可能希望通过打印出列表值来查看从文本框中实际收集到的内容:

        for (int i = 0; i < list2.Count; i++) {
            Console.WriteLine($"Item {i+1}: '{list2[i]}'");
        }

也许您在其中一个文本框中输入了一个空格。
请记住,1 one 不等于例如 1 one1 one (注意空格)。

【讨论】:

  • 请注意,第二个WriteLine 调用会输出列表中由单引号括起来的项目。这是此类问题输出的重要部分,因为它将清楚地显示诸如前导或尾随空格或换行符之类的内容。
【解决方案3】:

您的应用程序代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WinForms_ExchangeTextsInTextBoxes
{
    public partial class Form2 : Form
    {
        /// <summary>
        /// list of call numbers created
        /// </summary>
        public static List<string> values = new List<string>
        {
            "001.45 EBC",
            "004.56 VWH",
            "002.44 MFH",
            "003.88 CSK",
            "006.96 FEB",
            "008.77 NJC",
            "005.23 MKF",
            "007.62 IJN",
            "010.13 FNH",
            "009.88 ENC"
        };


        public static List<string> sortedListBoxValues = new List<string> { };

        public Form2()
        {
            InitializeComponent();
        }

        private void Btn_Generate_Click(object sender, EventArgs e)
        {
            //clears items in the list box
            listBox1.Items.Clear();

            //random class created
            Random random = new Random();

            //for loop created that adds 10 random numbers to listbox
            for (var i = 0; i < 10; i++)
            {
                int index = random.Next(values.Count);

                //adds items to listbox
                listBox1.Items.Add(values[index]);

                // Add values to the sortedList
                sortedListBoxValues.Add(values[index]);
            }

            // Sort sorted list of listBox values
            sortedListBoxValues.Sort();
        }

        private void Btn_Check_Click(object sender, EventArgs e)
        {
            //list created
            List<string> textBoxValues = new List<string>();

            //adds each textbox to the list
            textBoxValues.Add(textBox1.Text);
            textBoxValues.Add(textBox2.Text);
            textBoxValues.Add(textBox3.Text);
            textBoxValues.Add(textBox4.Text);
            textBoxValues.Add(textBox5.Text);
            textBoxValues.Add(textBox6.Text);
            textBoxValues.Add(textBox7.Text);
            textBoxValues.Add(textBox8.Text);
            textBoxValues.Add(textBox9.Text);
            textBoxValues.Add(textBox10.Text);

            if (sortedListBoxValues.SequenceEqual(textBoxValues))
            {
                //disaplays a message to the user if the ordering was correct
                MessageBox.Show("sucess");
            }
            else
            {
                //displays a message to the user if the ordering was incorrect
                MessageBox.Show("fail");
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2013-10-11
    • 2019-08-14
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2019-11-11
    相关资源
    最近更新 更多