简体   繁体   中英

minimum and maximum numbers from user input

I found this code for finding minimum and maximum numbers from user input.

using System;
    class FindMinAndMaximalNumber
    {
        static void Main()
        {
            string[] numbers;
            bool isInteger=true;
            int minimal=int.MaxValue;
            int maximal=int.MinValue;
            Console.Write("Enter a sequence of numbers delimited with \",\":");
            numbers = (Console.ReadLine()).Split(',');

               int[] intNumbers=new int[numbers.Length];
            for (int i = 0; i < (numbers.Length); i++)
            {
                isInteger = int.TryParse(numbers[i], out intNumbers[i]);
                if (isInteger==false)
                {
                    break;
                }
            }
            if (isInteger)
            {
                for (int i = 0; i < numbers.Length; i++)
                {
                    if (intNumbers[i] < minimal)
                    {
                        minimal = intNumbers[i];
                    }
                    if (intNumbers[i] > maximal)
                    {
                        maximal = intNumbers[i];
                    }
                }
                Console.WriteLine("minimal={0}",minimal);
                Console.WriteLine("maximal={0}",maximal);
            }
            else
            {
                Console.WriteLine("Not a valid entry! Some of the entries are not integer!");
            }
        }
    }

My questions are:
1 - Is the following code declaring an array intNumbers that has the size equal to the size of Array called "number"

int[] intNumbers=new int[numbers.Length];

yes or no?

2 - Check this TryParse method....

isInteger = int.TryParse(numbers[i], out intNumbers[i]);

If IsInteger is true, then does it mean..that the value of i is stored in intNumbers[i] ...???? or can anyone explain this code breifly

  1. Yes. datatype[] arrayName = new datatype[length];

  2. The method int.TryParse() converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeede. In this case TryParse will check each element of array "numbers" and if it is a int datatype then it will store the value to other array. That have been done to prevent entering of not numerical values. So, if IsInteger is true then it means that all the values you entered are numbers

Thank you so much SimpleVar and Thank you soo much MegaTron for your help. Just last clarification .So it means that following code is "only" declaring the size of the new array called "intNumbers"

int[] intNumbers=new int[numbers.Length];

but the items are stored in this new array "intNumbers" by this code(along with the for loop)

isInteger = int.TryParse(numbers[i], out intNumbers[i]);

each time its true and for loop is executed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM