【问题标题】:Stuck on how to create DeleteBelow/Above and CountBelow/Above Methods for a NumberList坚持如何为 NumberList 创建 DeleteBelow/Above 和 CountBelow/Above 方法
【发布时间】:2019-03-12 14:39:44
【问题描述】:

大家好,我正在尝试创建一个 NumberList 类,它将跟踪任意数量的浮点值的列表。还能够使用私有列表对这些值执行操作以跟踪数字。

我在如何创建四种方法时遇到了麻烦:

  • void DeleteBelow(浮动阈值)

此方法从列表中删除所有低于阈值的数字。如果 myList 是包含 {1, 2, 3} 的 NumberList,则调用 myList.DeleteBelow(1.5) 将删除 1,只留下 {2, 3}。

  • void DeleteAbove(浮动阈值)

此方法从列表中删除所有高于阈值的数字。如果 myList 是包含 {1, 2, 3} 的 NumberList,则调用 myList.DeleteAbove(1.5) 将删除 2 和 3,只留下 {1}。

  • int CountBelow(浮动阈值)

此方法计算列表中有多少数字低于阈值。如果 myList 是包含 {1, 2, 3} 的 NumberList,则调用 myList.CountBelow(2.5) 将返回 2。

  • int CountAbove(浮动阈值)

此方法计算列表中有多少数字高于阈值。如果 myList 是包含 {1, 2, 3} 的 NumberList,则调用 myList.CountAbove(2.5) 将返回 1。

我想知道是否有人可以帮助我/为我指明正确的方向?

~干杯

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Numbers

{

public class NumberList
{
    // Your private List<float> field should go here
    private List<float> myList;

    public void Add(float number)
    {
        myList.Add(3);
    }

    public List<float> Numbers()
    {
        return myList;
    }
    public float Minimum()
    {
        return myList.Min();
    }
    public float Maximum()
    {
        return myList.Max();
    }
    public float Sum()
    {
        return myList.Sum();
    }
    public float Average()
    {
        return myList.Sum() /myList.Count();
    }
public int Count()
    {
             return myList.Count(); 
    }
public void DeleteBelow(float threshold)
    {
      //       myList.DeleteBelow(1.5) 
    }

public void DeleteAbove(float threshold)
    {
//       myList.DeleteAbove(1.5);
    }
    public int CountBelow(float threshold)
    {
       int CountBelow = 0;
        for (int i = myList.Count is; i > 0; i--)
        {
            if (NumberList(i) > threshold) ;
            {
                //...
            }
        }
    public int CountAbove(float threshold)
    {
//      return myList.CountAbove(2.5);
    }
}

【问题讨论】:

  • Add 中有一个错误,它总是添加数字 3 而不是给定的数字。

标签: c#


【解决方案1】:
public void DeleteBelow(float threshold)
{
          myList.RemoveAll(x => x < threshold); 
}

public void DeleteAbove(float threshold)
{
          myList.RemoveAll(x => x > threshold); 
}

public int CountBelow(float threshold)
{
     return myList.Count(x => x < threshold);
}

public int CountAbove(float threshold)
{
     return myList.Count(x => x > threshold);
}

【讨论】:

  • 最好使用myList.RemoveAll(predicate) 而不是手动循环。此外,您的 remove 方法将引发异常,因为您在迭代列表时正在修改列表,至少在其上调用 .ToList() 并枚举它,但 RemoveAll 的性能要高得多。
【解决方案2】:

您可以使用 foreach 循环遍历列表的所有项目,然后您可以检查其中的每个元素是否高于阈值。什么时候做你想做的事。

【讨论】:

    【解决方案3】:

    我会培养你跟随immutability的道路。它将使您的课程更易于维护,因为它将使您免受讨厌的副作用(如果您使用的是myList.Remove(),通常在DeleteBelowDeleteAbove 中)。 我还简化了Average,因为 LINQ 已经有这样的方法可用。

    public sealed class NumberList
    {
        private readonly IEnumerable<float> myList;
        public NumberList(IEnumerable<float> numbers) => myList = numbers;
        public NumberList Add(float number) => new NumberList(myList.Concat(new[] { number }));
        public List<float> Numbers() => myList.ToList();
        public float Minimum() => myList.Min();
        public float Maximum() => myList.Max();
        public float Sum() => myList.Sum();
        public float Average() => myList.Average();
        public int Count() => myList.Count();
        public int CountBelow(float threshold) => myList.Count(n => n < threshold);
        public int CountAbove(float threshold) => myList.Count(n => n > threshold);
        public NumberList DeleteBelow(float threshold) => new NumberList(myList.Where(n => n >= threshold));
        public NumberList DeleteAbove(float threshold) => new NumberList(myList.Where(n => n <= threshold));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-26
      • 2017-04-12
      • 2022-11-11
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多