如何确定三个整数是否都相等

本文关键字:是否 整数 三个 何确定 | 更新日期: 2023-09-27 18:14:19

我有三个int值:value1, value2和value3

我如何最好地确定它们是否都是相同的?

我试着:

return value1 == value2 == value3

但是这里说:

操作符'=='不能用于'bool'和'int'类型的操作数。

所以我猜它比较了前两个返回一个布尔值并试图将其与第三个进行比较

I could go:

return value1 == value2 && value2 == value3;

但是这里似乎越来越乱了。

谁有好的建议?

如何确定三个整数是否都相等

第二种我觉得还可以。

随着列表变长,这可能会变得难以处理。在这种情况下,我将按照AllSame的行编写一个扩展方法。

bool AllSame(this IEnumerable<int> list)
{
    bool first = true;
    int comparand = 0;
    foreach (int i in list) {
       if (first) comparand = i;
       else if (i != comparand) return false;
       first = false;
    }
    return true;
}

或者使用params关键字:

bool AllSame(params int[] list)
{
    return (list as IEnumerable<int>).AllSame();
}

那么你可以直接写:

if (AllSame(value1, value2, value3, value4, value5)) ...

我觉得还行。我唯一的意见是,你应该为这个方程引入一个"解释变量"。除了解释计算之外,在检查结果时,返回值现在为断点或跟踪点提供了一个很好的位置。

bool allThreeAreEqual = value1 == value2 && value2 == value3;
return allThreeAreEqual;

我修改了我的原始答案,包括一个更通用的方法,不依赖于LINQ或扩展方法。我认为可以肯定的是,这种方法的性能会更高,因为它不需要枚举整个列表来确定列表中早期存在不同值时的唯一性。

class Program 
{ 
    static void Main(string[] args) 
    { 
        int value1 = 1, value2 = 2, value3 = 1; 
        Console.WriteLine(AllAreEqual<int>(value1, value2, value3));
        Console.Write("V2: 1 value ");
        Console.WriteLine(AllAreEqual_V2<int>(1));
        Console.Write("V2: no value ");
        Console.WriteLine(AllAreEqual_V2<int>());
        Console.Write("V2: 3 values, same ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1, 1));
        Console.Write("V2: 3 values, different ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1, 2));
        Console.Write("V2: 2 values, same ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1));
        Console.Write("V2: 2 values, different ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 2));
        Console.ReadKey(); 
    } 
    static bool AllAreEqual<T>(params T[] args) 
    { 
        return args.Distinct().ToArray().Length == 1; 
    }
    static bool AllAreEqual_V2<T>(params T[] args)
    {
        if (args.Length == 0 || args.Length == 1)
        {
            return true;
        }
        if (args.Length == 2)
        {
            return args[0].Equals(args[1]);
        }
        T first = args[0];
        for (int index = 1; index < args.Length; index++)
        {
            if (!first.Equals(args[index]))
            {
                return false;
            }
        }
        return true;
    }
}

如果您只是在寻找优雅(考虑到您已经有了一个没有任何问题的解决方案),您可以使用优秀的LINQ。可以处理三个或更多。

class Program
    {
        static void Main(string[] args)
        {
            List<int> mylist = new List<int>();
            mylist.Add(1);
            mylist.Add(1);
            mylist.Add(1);
            mylist.Add(1);
            bool allElementsAreEqual = mylist.All( x => ( x == mylist.First() ));
        }
    }

你也可以这样做

bool AllSame(int a, int b, int c, int comparand) {
    return board[a] == comparand && board[b] == comparand && board[c] == comparand;
}
int nOneInput = 5;
int nTwoInput = 5;
int nThreeInput = 5;
if ((nOneInput + nTwoInput + nThreeInput ) / 3 ==  nOneInput ) 
{
    // all 3 sides are equal when...
    // the sum of all 3 divided by 3 equals one of the values 
}

使用LINQ最好是我们Any()。为什么Any()而不是All()是因为All()将在集合的所有项目上测试谓词,而Any()将在项目匹配谓词时立即退出。

这里我使用反向检查。我正在寻找与项目"a"不同的任何项目。所以一旦它找到一个不同的我们就知道它们不都是相等的所以它退出并返回真。因此,它将只测试项目"b","c"answers"d"。

// all values to compare
var a = 4;
var b = 4;
var c = 4;
var d = 8;
var e = 6;
var f = 4;
// return if any of the following is different and negate to get a true
var areSame = (!new[] { b, c, d, e, f}.Any(i => i != a));

如果你重写等于,你可以创建通用的扩展,这是可重用的,可以为多种类型工作,只要他们实现IEqualityComparer<T>

这就是我要做的

if((value1 == value2) && (value1 == value3) && (value2 == value3))
{
    //Whatever you want here
}

接受的答案很好,但我也需要支持不同的类型,这是通用版本

      public static bool AllSame<T>(params T[] items)
         {
            var first = true;
            T comparand = default;
            foreach (var i in items)
            {
               if (first) comparand = i;
               else if (!i.Equals(comparand)) return false;
               first = false;
            }
            return true;
         }

用法:

if (AllSame(true, false, true)) // false
if (AllSame(5, 5, 5)) // true
if (AllSame(record1 , record2, record3))