【问题标题】:Windows forms calculator algorithms. Sorting and highest/lowest numbersWindows 窗体计算器算法。排序和最高/最低数字
【发布时间】:2018-04-19 05:39:06
【问题描述】:

我目前正在使用计算器,但我的 2 个算法无法正常工作。

  1. 在我的历史记录(列表框)中,我从计算器中获得了数字,并且我有一个底部可以找到最小的数字。我的代码出错了。

  2. 我想要一个 [排序] 底部,用于对数字进行升序或降序排序。我已经尝试过 listbox1.sorted 但我只能让它按字母顺序工作。

如果您知道我对 nr.1 做错了什么或知道如何修复排序算法,请告诉我。

 int count = 0;
 int tal = 0;
 double Mtal = 999999999999999999;
 bool hit;
 int count1 = 0;
 private void button26_Click(object sender, EventArgs e)
    {
        while (count < 100)
        {
            foreach (var Listboxitem in listBox1.Items)
            {
                hit = false;
                if (Convert.ToDouble(Listboxitem) < Mtal)
                {

                    Mtal = Convert.ToDouble(Listboxitem);
                    hit = true;
                }
                count = count + 1;
                if (hit)
                {
                    count1 = count;
                }
            }
        }
        this.listBox1.SelectedIndex = count1 - 1;

    }

【问题讨论】:

  • 无法将类型“double”隐式转换为“int”。存在显式转换(您是否缺少演员表?我得到的错误是 btw
  • @EmilSjödin 运行您在此处发布的代码我无法重现您的问题,所以..

标签: c# algorithm sorting


【解决方案1】:

您在 ListBox 中的值是整数。因此,将变量Mtal 的声明从double 更改为int。然后,而不是Convert.ToDouble() 使用int.Parse(),因为您需要整数来与现有的最小值进行比较。 像这样的东西应该适合你:

 int count = 0;
 int tal = 0;
 int Mtal = int.MaxValue;
 bool hit;
 int count1 = 0;
 private void button26_Click(object sender, EventArgs e)
 {
     while (count < 100)
     {
          foreach (var Listboxitem in listBox1.Items)
          {
             hit = false;
             if (int.Parse(Listboxitem.ToString()) < Mtal)
             {

                 Mtal = int.Parse(Listboxitem.ToString());
                 hit = true;
             }
             count = count + 1;
             if (hit)
             {
                count1 = count;
             }
        }
    }
    this.listBox1.SelectedIndex = count1 - 1;

 }

然后对于订购,我建议您在List&lt;ListItem&gt; 上使用 LINQ。以您为例:

private void button_OrderByDescencing_Click(object sender, EventArgs e)
 {
        List<ListItem> items= new List<ListItem>();
        foreach (ListItem a in lb.Items)
        {
            items.Add(a);
        }
        items=items.OrderByDescending(a => int.Parse(a.Value)).ToList();
        foreach (ListItem a in items)
        {
             listBox1.Items.Add(a);
        }

 }

对于升序:

 private void button_OrderByAscending_Click(object sender, EventArgs e)
 {
        List<ListItem> items= new List<ListItem>();
        foreach (ListItem a in lb.Items)
        {
            items.Add(a);
        }
        items= items.OrderBy(a => int.Parse(a.Value)).ToList();
        foreach (ListItem a in items)
        {
             listBox1.Items.Add(a);
        }

 }

【讨论】:

  • 感谢您的快速回答@Tadej 1 我得到的代码问题是它想从对象转换为字符串。 (论据 1:不能从 'object' 转换为 'string')
  • @EmilSjödin 查看我所做的编辑。现在这两个问题都应该解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2021-03-10
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
相关资源
最近更新 更多