使用 If 比较多个值,否则 if

本文关键字:否则 if If 比较 使用 | 更新日期: 2023-09-27 18:33:31

我对使用if有点困惑,否则if在我当前的项目中...我需要比较 6 个值,最小的值应该由消息框提示。.这是我的代码

if ((sort1 > sort2) || (sort1 > sort3) || (sort1 > sort4) || (sort1 > sort5))
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Insertion Sort with the time of " + elapsedMs1 + " ms");
}
else if ((sort2 > sort1) || (sort2 > sort3) || (sort2 > sort4) || (sort2 > sort5))
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Selection Sort with the time of " + elapsedMs2 + " ms");
}
else if ((sort3 > sort1) || (sort3 > sort2) || (sort3 > sort4) || (sort3 > sort5))
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Bubble Sort with the time of " + elapsedMs3 + " ms");
}
else if ((sort4 > sort1) || (sort4 > sort2) || (sort4 > sort3) || (sort4 > sort5))
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Merge Sort with the time of " + elapsedMs4 + " ms");
}
else if ((sort5 > sort1) || (sort5 > sort2) || (sort5 > sort3) || (sort5 > sort1))
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Quick Sort with the time of " + elapsedMs5 + " ms");
}

但它不会提示消息框中的最小数字。这是否有更正确的编码,而不是使用 if,否则 if?我也尝试过使用 AND,但它也不起作用。.

使用 If 比较多个值,否则 if

丢弃此代码并改用循环。

  • 将结果放在一个数组中。
  • 使用变量int largest_value = 0
  • 使用变量int largest_index = -1
  • 遍历数组,对于每个结果,检查它是否大于"largest_value"。
  • 如果值较大,则设置largest_value =值,并设置largest_index = i; ,其中i是循环迭代器。

循环后,largest_value将包含最大值,largest_index将指出该值在数组中的位置。

我会将所有结果存储在字典中,如下所示(数字由组成):

Dictionary<string, int>  myDictionary = new Dictionary<string, int>()
{
    {"Insertion Sort", 12},
    {"Selection Sort ", 35},
    {"Bubble Sort", 42},
    {"Merge Sort", 52},
    {"Quick Sort ", 32}
};
var min = myDictionary.First(kv => kv.Value == myDictionary.Values.Min());
Console.Out.WriteLine("The fastest is " + min.Key + " with a time of " + min.Value + "ms");

这将允许您在不更改 if/else 结构的情况下测试尽可能多的算法(因为您不使用它)。

输出为:

最快的是插入排序,时间为 12ms

我建议使用字典Linq

var algorithms = new Dictionary<String, Double>() { // or Dictionary<String, int>
  {"Insertion Sort", sort1},
  {"Selection Sort", sort2},
  ...
};
var best = algorithms
  .OrderBy(pair => pair.Value)
  .First();
MessageBox.Show(String.Format("The Best Sorting Technique Algorithm is {0} with the time of {1} ms", best.Key, best.Value));

使用 Linq 的优点是可以轻松创建自己喜欢的报告,例如,让我们从最快到最慢打印出算法:

var data = algorithms
  .OrderBy(pair => pair.Value)
  .Select(pair => String.Format("{0} took {1} ms", pair.Key, pair.Value));
MessageBox.Show(String.Join(Environment.NewLine, data));

这不是else if的问题,而是您使用的运算符的问题 - 如果排序算法比所有其他算法都快,而不仅仅是一种,那么排序算法是最快的。

因此,更改条件以使用 && 运算符而不是 ||

请注意,这不是最好的解决方案(请参阅其他人的评论),它只是您的代码的狐狸。

我想你的意思如下:)

if ( !( sort1 < sort2 ) && !( sort1 < sort3 ) && !( sort1 < sort4 ) && !( sort1 < sort5 ) )
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Insertion Sort with the time of " + elapsedMs1 + " ms");
}
else if ( !( sort2 < sort3 ) && !( sort2 < sort4 ) && !( sort2 < sort5 ) )
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Selection Sort with the time of " + elapsedMs2 + " ms");
}
else if ( !( sort3 < sort4 ) && !( sort3 < sort5 ) )
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Bubble Sort with the time of " + elapsedMs3 + " ms");
}
else if ( !( sort4 < sort5 ) )
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Merge Sort with the time of " + elapsedMs4 + " ms");
}
else 
{
    MessageBox.Show("The Best Sorting Technique Algorithm is Quick Sort with the time of " + elapsedMs5 + " ms");
}

我想 sort1、sort2 等的值可以彼此相等。在这种情况下,将选择具有最低变量名称的值:)

将每个 || 更改为 &&&您希望所有这些都为真。

将每个>都更改为<,因为您想要最小的,而不是最大的。

您在最后一个 if 中也有拼写错误:您有 sort5> sort1 两次,而您没有 sort5>sort4