代码之家  ›  专栏  ›  技术社区  ›  SystemX17

C#从4个最大/最小选择中找到2个最强值

  •  0
  • SystemX17  · 技术社区  · 7 年前

    所有值的范围为-100.0f到100.0f

    public float happysad = 0.0f;
    public float passiveagressive = 0.0f;
    public float friendenemy = 0.0f;
    public float weakstrong = 0.0f;
    
    void GetStrongestTwo() {
        /* Work out the 2 most extreme values
        if (happysad > 0){
                sad is the strongest
        }
        if (happysad < 0){
                sad is the strongest
        }
        Need to do this for all then work out the two that have the largest (either positive or negative value)
        */ 
    }
    

    我已经尝试了常数if语句,但根据我对max和min的理解,应该有一种更简单的方法。我的尝试产生了24条if语句。另一种方法是将这些值分离为以下值。

    public float happy = 0.0f;
    public float sad = 0.0f;
    public float passive = 0.0f;
    public float agressive = 0.0f;
    public float friend = 0.0f;
    public float enemy = 0.0f;
    public float weak = 0.0f;
    public float strong = 0.0f;
    

    我的问题是,应对这一挑战的最佳方法是什么?如果有一种编码方法存在,我只是需要更多的研究,我希望朝着正确的方向推进,或者如果第二种解决方案更可行,那么我将在稍后的代码中对此进行补偿。由于这些值是相反的,我宁愿每次发生影响情感元素的事件时都必须添加或删除1.0f的值。

    2 回复  |  直到 7 年前
        1
  •  2
  •   imerso    7 年前

    不要使用那么多变量和很多if,而是创建一个具有名称和值的简单类,如:

    public class GameInfo
    {
      public string name;
      public float value;
      public GameInfo(string name, float value)
      {
        this.name = name;
        this.value = value;
      }
    }
    

    已排序 列出这些值。这样地:

        List<GameInfo> info = new List<GameInfo>();
    
        // please add the other info you wish
        info.Add(new GameInfo("happy", 5.0f));
        info.Add(new GameInfo("sad", 15.0f));
        info.Add(new GameInfo("passive", 4.0f));
        info.Add(new GameInfo("agressive", 35.0f));
        // ...
    
        // sort the list (I used linq but you could use other methods)
        List<GameInfo> sortedInfo = info.OrderByDescending(o => o.value).ToList();
    
        foreach (GameInfo i in sortedInfo)
        {
            Console.WriteLine(i.name + ", " + i.value);
        }
    

    就这样。

        2
  •  0
  •   FarmerBob    7 年前

    你说的“最强”是指你试图找到具有最高量级的值吗?你只需要变量的值或名称吗?

    如果您只需要这些值,可以执行以下操作:

    float happysad = -10.0f;
    float passiveaggressive = 5.0f;
    float friendenemy = 2.0f;
    float weakstrong = 7.0f;
    
    var twoStrongest = 
        new [] {happysad, passiveaggressive, friendenemy, weakstrong}
            .Select(stat => new {Value = stat, Magnitude = Math.Abs(stat)})
            .OrderByDescending(statWithMagnitude => statWithMagnitude.Magnitude)
            .Select(statWithMagnitude => statWithMagnitude.Value)
            .Take(2).ToList();
    

    上面所做的:将每个浮点映射到一个临时对象,该对象具有每个统计的幅值,按幅值排序,然后将其转换回原始浮点,并获取前两个值。

    另一方面,如果要查找名称,可以将名称/状态映射存储在字典中:

    var stats = new Dictionary<string, float> {
        {"happysad",  -10.0f},
        {"passiveaggressive",  -6.0f},
        {"friendenemy",  8.0f},
        {"weakstrong",  3.0f}
    };
    
    var twoStrongest = stats
        .Select(entry => new {Entry = entry, Magnitude = Math.Abs(entry.Value)})
        .OrderByDescending(statWithMagnitude => statWithMagnitude.Magnitude)
        .Select(statWithMagnitude => statWithMagnitude.Entry)
        .Take(2).ToList();