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

时间:2014-11-05 04:44:15

标签: c#

我制作了一个程序,要求您输入少于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("");
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想拥有整数列表中最小和最大的条目? 在这种情况下,您只需对列表进行排序并检索已排序列表的第一个/最后一个条目:

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

那是你在找什么?

答案 1 :(得分:0)

所以你有一个整数列表(数字)。范围在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());

答案 2 :(得分:0)

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); 

会写(我替换你的foreach):

            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());