【问题标题】:Check if multiple bool variables have specific value检查多个布尔变量是否具有特定值
【发布时间】:2015-12-13 19:14:34
【问题描述】:

在我当前的应用程序中,我有 6 个玩家,每个人都有 1 个布尔变量。在某些情况下设置为真(最初它们是假的)。问题是我想检查哪些 5 个变量设置为真,哪个设置为假,但我想不出任何好主意。 . 只有一些 if 语句检查每个组合

if(a && b && c && d && e && !f)
{
    //f is false in this case and I will do some operations here
}

然而,这是有史以来最丑陋且写得不好的代码。更通用的方法是什么?

【问题讨论】:

  • 你能发布包含布尔属性的类+一些执行代码吗?

标签: c# boolean


【解决方案1】:

仅使用布尔值将很难做到这一点。但是,如果您将布尔值与一些其他数据一起包装在一个类中,就会变得更容易。

class Item
{
    public bool   IsCondition {get; set;}
    public string Name        {get; set;}
}

var itemsToCheck = new List<Item>()
{
    new Item { IsCondition = true;  Name = "A",
    new Item { IsCondition = true;  Name = "B",
    new Item { IsCondition = false; Name = "C",
    new Item { IsCondition = true;  Name = "D",
}
foreach(var item in itemsToCheck)
{
    if(!Item.IsCondition)
    {
        Console.WriteLine($"Item {item.Name} is false"); 
    }
}

您还可以使用 Linq 获取所有错误的列表

var items = itemsToCheck.Where(i => !i.IsCondition);

或者,如果你知道永远只有一个是假的,你就可以得到那个单一的项目。

var item = itemsToCheck.Where(i => !i.IsCondition).Single();

因此有两个要点:

  • 您应该将相似数据集存储在一个集合中,例如一个列表
  • 如果您想将一些信息组合在一起,请使用类。

【讨论】:

  • 感谢您的快速回答!这将完成这项工作,我会尽快将其标记为答案。
【解决方案2】:

您可以为它们分配布尔列表,然后使用它们。

List<bool> bools = new List<bool> {a,b,c,d,e,f};

if (bools.Count(x => x) == 5) // if there are 5 true items
{
    int index = bools.IndexOf(false); // index of false item
    // do your things here.
}

请记住,索引是基于 0 的。表示索引 0 指的是第一项。

【讨论】:

    【解决方案3】:

    通常你会使用数组/列表,只计算 false 值:

    var onlyOneFromListIsFalse = players.Select(p => !p.SomeProperty).Count() == 1;
    

    您可以对单个变量使用类似的方法

    var onlyOneVariableIsFalse = ((a ? 0 : 1) + (b ? 0 : 1) ... (f ? 0 : 1)) == 1;
    

    【讨论】:

      【解决方案4】:

      使用LINQ 和 List/Array 将大大减少您的代码。

      using System;
      using System.Linq;
      using System.Collections.Generic;
      
      public class Program
      {
          public static void Main()
          {
              var players = new List<Player>
              {
                  new Player("Orel", true),
                  new Player("Zeus"),
                  new Player("Hercules", true),
                  new Player("Nepton"),
              };
      
              var playingPlayers = players.Where(p => p.IsPlaying);
              foreach (var player in playingPlayers)
              {
                  Console.WriteLine(player.Name);
              }
          }
      }
      
      public class Player
      {
          public string Name { get; set; }
          public bool IsPlaying { get; set; }
      
          public Player(string name, bool isPlaying = false)
          {
              Name = name;
              IsPlaying = isPlaying;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-15
        • 1970-01-01
        • 2016-08-22
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多