C# GUI application that stores an array and displays the highest and lowest numbers by clicking a button

后端 未结 2 1070
故里飘歌
故里飘歌 2021-01-29 08:43

Background:
This is updated from 13 hours ago as I have been researching and experimenting with this for a few. I\'m new to this programming arena so I\'ll b

相关标签:
2条回答
  • 2021-01-29 08:56

    This looks like homework, so you should try a bit more than that. Here is what you could do: parse the string (say it's a comma-separated list of numbers), cast each value to int and populate your array. You can either call .Max() / .Min() methods or loop through the values of the array and get the max / min value. Here is a bit of code:

    int n = 10;
    int[] numbers = (from sn in System.Text.RegularExpressions.Regex.Split(inputText.Text, @"\s*,\s*") select int.Parse(sn)).ToArray();
    int max = numbers.Max();
    int min = numbers.Min();
    //int max = numbers[0];
    //int min = numbers[0];
    //for(int i = 1; i < n; i++)
    //{
    //    if(max < numbers[i])
    //    {
    //        max = numbers[i];
    //    }
    //    if(min > numbers[i])
    //    {
    //        min = numbers[i];
    //    }
    //}
    
    0 讨论(0)
  • 2021-01-29 09:02

    This in all probability being a homework I will not provide entire solution but just provide a hint.

    Task to me seems to be to some how accept 10 integers and then show smallest and largest of them. For this there is no need to maintain an array (off-course only if maintaining an array is itself not part of the problem). You just need to keep track of current minimum and current maximum.

    As and when you receive an input compare it with the current minimum and maximum and update them accordingly. e.g.

    if(num < curr_min) curr_min = num;
    
    0 讨论(0)
提交回复
热议问题