繁体   English   中英

如何检查文本框中的字符串是否按升序排列

[英]How to check if strings in textboxes are in ascending order

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

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

第一张图片显示字符串按升序排列并显示成功消息,请注意,显示成功消息的唯一原因是因为用户在文本框中输入了列表框中存在的所有值,而不是因为它们按升序排列

在第二张图片中,如果您查看前两个文本框,您会看到第一个值和第二个值已被交换。这意味着输入的值不再按升序排列,因此应显示失败消息,但应用程序不会显示检查值是否正确排序并且仍然显示成功消息

最后一张图片显示,如果在任何文本框中输入列表框中不存在的值,将显示失败消息。这应该会发生,但是如果顺序不是升序,也应该出现失败消息

   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

正如上面的人所说:如果你想让它按预期工作,你必须删除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 (注意空格)。

您的申请代码如下:

在此处输入图像描述

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");
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM