如何添加查找最大值和最小值并显示它们的函数

本文关键字:显示 最小值 函数 最大值 何添加 添加 查找 | 更新日期: 2023-09-27 18:29:06

我制作了一个程序,要求您输入几个小于100的数字,然后取下您输入的数字,告诉哪些是有效的条目。我想添加的是一个功能,它可以获取那些有效的条目,并找到最小和最大的数字。在它找到最小和最大的值后,我希望它们显示在上面写着"无效条目:"的地方。有人能帮我添加这个吗?

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Programming_Exercise_2_Chapter_6
    {
        class Program
        {
            static void Main(string[] args)
            {
                string answer;
                do
                {      
                    Console.Clear();
                    Header();
                    int number;
                    string indata;
                    List<int> validEntries = new List<int>();
                    List<string> invalidEntries = new List<string>();
                    while (true)
                    {
                        Console.WriteLine("Insert numbers less than 100: ");
                        indata = Console.ReadLine();
                        if (Int32.TryParse(indata, out number))
                        {
                            if (number <= 100 && number > 0)
                                validEntries.Add(number);
                             else
                                invalidEntries.Add(number.ToString());
                        }
                        else
                            invalidEntries.Add(indata);
                        Console.WriteLine("Press N to stop. Press enter to continue.");
                        indata = Console.ReadLine();
                        Console.Clear();
                        if (indata == "n"|| indata == "N")
                            break;
                    }            
                    DisplayEntries(validEntries, invalidEntries);
                    Console.ReadLine();
                    Console.WriteLine("Do you want to try again?(Enter Y for Yes, or N for       No)");
                    answer = Console.ReadLine();
                }
                while (answer == "Y" || answer == "y");
            }
            static void DisplayEntries(List<int> validEntries, List<string> invalidEntries)
            {
                Console.WriteLine("Your valid entries were: ");
                foreach (int i in validEntries)
                    Console.WriteLine(i);
                Console.WriteLine();
                Console.WriteLine("Your invalid entries were: ");
                foreach (string s in invalidEntries)
                    Console.WriteLine(s);
            }
            static void Header()
            {
                Console.WriteLine("'tNumber Validation App");
                Console.WriteLine("Please enter a few numbers less than 100.'nValid entries     will be displayed.");
                Console.WriteLine("");
            }
        }
    }

如何添加查找最大值和最小值并显示它们的函数

如果我理解你的问题,你想从整数列表中得到最小和最大的条目吗?在这种情况下,您可以简单地对列表进行排序,并检索排序列表的第一个/最后一个条目:

validEntries.Sort();
var smallest = validEntries.First();
var highest = validEntries.Last();

这就是你想要的吗?

所以您有一个整数(数字)列表。范围在1和100之间。所有存储在validEntries中的。因此,你必须浏览列表中的所有数字,当你找到一个高数字时,你会将其存储起来,并将其与列表中的下一个数字进行比较。

int highest_nr = 0;
int lowest_nr  = 100;
foreach (int i in validEntries)
{
    if (i < lowest_nr)
        lowest_nr = i
    if (i > highest_nr)
        highest_nr = i;
    Console.WriteLine(i);
}
Console.WriteLine("Highest number = " + highest_nr.ToString());
Console.WriteLine("Lowest number  = " + lowest_nr.ToString());

validEntries.Max()validEntries.Min()将分别获得最高值和最低值。

        Console.WriteLine("Your valid entries were: ");
        foreach (int i in validEntries)
            Console.WriteLine(i);
        Console.WriteLine();
        Console.WriteLine("Your invalid entries were: ");
        foreach (string s in invalidEntries)
            Console.WriteLine(s); 

将被写入(我替换了你的前臂):

            Console.WriteLine("Your valid entries were: ");
            Console.WriteLine(string.Join(Environment.NewLine, validEntries));
            Console.WriteLine();
            Console.WriteLine("Your invalid entries were: ");
            Console.WriteLine(string.Join(Environment.NewLine, invalidEntries));
            Console.WriteLine();
            Console.WriteLine("Min:{0}  Max:{1}",validEntries.Min(),validEntries.Max());