如何从列表中打印几个最低的数字

本文关键字:几个 数字 列表 打印 | 更新日期: 2023-09-27 17:49:13

我写了以下代码:

static void Main(string[] args)
{
    int num1 = 0;
    for (int i = -1; i < 10; i++)
    {
        Console.Write("   ");
        if (num1 < -1)
        {
            Console.WriteLine("You entered a number less than -1, please enter higher number");
            Console.Write("   ");
        }
        num1 = int.Parse(Console.ReadLine());
    }
    Console.Read();
}

用户可以输入任何他想要的数字,只要这个数字大于-1(数字-1也可以由用户书写)然后我需要打印4个最低的数字(不包括-1),例如:用户输入:200,10,50,- 1,5,6,2,8,3Thanit将打印:2,3,5,6

非常感谢你的帮助:)

谢谢

如何从列表中打印几个最低的数字

Where过滤掉-1,用OrderBy订购其余的,然后用Take订购你需要的

Console.WriteLine(string.Join(" ", list.Where(n => n >= 0).OrderBy(n => n).Take(4)));

注意,这是在您的名称空间中使用using System.Linq;

使用List(T).Sort()对列表进行排序,然后获得最低的数字:

static void Main(string[] args)
{
    int num1 = 0;
    List<int> numbers = new List<int>();
    for (int i = -1; i < 10; i++)
    {
        Console.Write("   ");
        if (num1 < -1)
        {
            Console.WriteLine("You entered a number less than -1, please enter higher number");
            Console.Write("   ");
            i--;
        }
        else
        {
            num1 = int.Parse(Console.ReadLine());
            numbers.Add(num1);
        }
    }

     //sorting
    numbers.Sort(); 
    int amountCounted = 0;
    for (int i = 0; i < numbers.Count && amountCounted < 5; i++)
    {
        if (numbers[i] > -1)
        {
            Console.WriteLine(numbers[i]);
            amountCounted++;
        }
        else
            i++;
    }
    Console.Read();
}

这段代码将循环,检查数字是否为-1,如果不是,打印它并增加amountCounted变量。

注意:List(T)需要System.Collections.Generic命名空间

编辑

根据您的注释,您将使用另一个变量来保存最低值。Examle:

static void Main(string[] args)
{
    int num1 = 0;
    int lowestNumber = 2147483647;//Max int value:)
    int secondLowest = 2147483647;        
    for (int i = -1; i < 10; i++)
    {
        Console.Write("   ");
        num1 = int.Parse(Console.ReadLine());
        if (num1 < -1)
        {
            Console.WriteLine("You entered a number less than -1, please enter higher number");
            Console.Write("   ");
        }
        else
        {
            if (num1 < lowestNumber && num1 > -1)
            {
                secondLowest = lowestNumber;
                lowestNumber = num1;
            }
            else if (num1 < secondLowest && num1 > -1)
                secondLowest = num1;
        }
    }
    Console.WriteLine(lowestNumber);
    Console.Read();
}

仅当数字大于-1时将它们全部存储在List中,然后在最后执行如下操作:

list = {200,10,50,5,6,2,8,3};

Console.WriteLine(string.Join(" ", list.OrderBy(x => x).Take(4).Select(x => x.ToString()).ToArray<string>()));

希望对大家有所帮助

这会给你你想要的。输入200 10 50 -1 5 6 2 8 3将打印:2, 3, 5, 6

void Main()
{
    Console.WriteLine("Please enter some numbers seperated by a space");
    Console.WriteLine("");
    var userInput = Console.ReadLine();
    var numbers = userInput.Split(' ');
    var lowestNumbers = numbers.Where(n => int.Parse(n) > -1).OrderBy(n => int.Parse(n)).Take(4).ToList();
    var output = string.Join(", ", lowestNumbers);
    Console.WriteLine(output);
}