如何找到最高和最低的数字C#

时间:2010-12-08 18:08:30

标签: c#

我从三个变量中得到三个值。如何查看谁是最高号码谁是最低号码?

数字表示如下:

private int _score1; 
private int _score2; 
private int _score2; 

代码:

Public int Highest
{
  return the highest number here;
}

public int Lowest
{
  return the lowest number here;
}

我可以计算构造函数中的最高和最低数字吗?

10 个答案:

答案 0 :(得分:8)

强制性的Linq回答:

Public int Highest(params int[] inputs)
{
  return inputs.Max();
}

public int Lowest(params int[] inputs)
{
  return inputs.Min();
}

这个的美妙之处在于它可以采用任意数量的整数输入。为了使其成为故障安全,您应检查null / empty输入数组(意味着没有任何内容传递给该方法)。

要在没有Linq的情况下执行此操作,您基本上只需要模仿扩展方法执行的逻辑:

Public int Lowest(params int[] inputs)
{
   int lowest = inputs[0];
   foreach(var input in inputs)
      if(input < lowest) lowest = input;
   return lowest;
}

同样,为了使它变得万无一失,你应该检查一个空或null输入数组,因为调用Lowest()会抛出一个ArrayIndexOutOfBoundsException。

答案 1 :(得分:7)

这是一种方法:

public int Highest
{
    get { return Math.Max(_score1, Math.Max(_score2, _score3)); }
}

public int Lowest
{
    get { return Math.Min(_score1, Math.Min(_score2, _score3)); }
}

答案 2 :(得分:4)

int[] numbers = new[] { _score1, _score2, _score3 };
int min = numbers.Min();
int max = numbers.Max();

答案 3 :(得分:3)

最高

return (x > y) ? (x > z ? x : z) : (y > z ? y : z)

最低

return (x < y) ? (x < z ? x : z) : (y < z ? y : z)

答案 4 :(得分:1)

如果您想简单地检查哪个是最高的,您可以这样做

private int _highest = _score1;  
if (_score2 > _highest)  
  _highest = _score2  
if (_score3 > _highest)  
  _highest = _score3

同样,你可以找到最低价

private int _lowest = _score1;  
if (_score2 < _lowest)  
  _lowest = _score2  
if (_score3 < _lowest)  
  _lowest = _score3

答案 5 :(得分:1)

这是你可以做的事情:

public class Numbers
{
    private int _number1;
    private int _number2;
    private int _number3;

    public readonly int Highest;
    public readonly int Lowest;

    public Numbers(int num1, int num2, int num3)
    {
        int high;
        int low;

        _number1 = num1;
        _number2 = num2;
        _number3 = num3;

        high = num1 > num2 ? num1 : num2;
        high = high > num3 ? high : num3;

        low = num1 < num2 ? num1 : num2;
        low = low < num3 ? low : num3;

        Highest = high;
        Lowest = low;
    }
}

答案 6 :(得分:0)

使用LINQ-to-Objects,你可以做这样的事情。

var numbers = new [] {_score1, _score2, _score3};
numbers.Sort();
var lowest = numbers.First();
var highest = numbers.Last();

答案 7 :(得分:0)

作为参考:在某些情况下,你将拥有三个以上的变量(可能不知道有多少)。如果它们存储在一个数组中,这就是这样做的方法:

int Highest(int[] numbers)
{
    int highest = Int32.MinValue();

    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] > highest)
            highest = numbers[i];
    }

    return highest;
}

int Lowest(int[] numbers)
{
    int lowest = Int32.MaxValue();

    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] < lowest)
            lowest = numbers[i];
    }

    return lowest;
}

这适用于任何长度的int数组。

答案 8 :(得分:0)

查找最大和最小的数字

using System;

namespace LargeSmall;

{
    class Program
    {
        public static void Main()
        {

            float large, small;
            int[] a = new int[50];
            Console.WriteLine("Enter the size of Array");
            int max = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the array elements");
            for (int i = 0; i < max; i++)
            {
                string s1 = Console.ReadLine();
                a[i] = Int32.Parse(s1);
            }
            Console.Write("");
            large = a[0];
            small = a[0];
            for (int i = 1; i < max; i++)
            {
                if (a[i] > large)
                    large = a[i];
                else if (a[i] < small)
                    small = a[i];
            }
            Console.WriteLine("Largest element in the array is {0}", large);
            Console.WriteLine("Smallest element in the array is {0}", small);
        }
    }

答案 9 :(得分:0)

这是找到最小数字的简单逻辑

输入:11,0,3,33输出:&#34; 0&#34;

namespace PurushLogics
{
    class Purush_SmallestNumber
    {
        static void Main()
        {
            int count = 0;
            Console.WriteLine("Enter Total Number of Integers\n");
            count = int.Parse(Console.ReadLine());

            int[] numbers = new int[count];

            Console.WriteLine("Enter the numbers"); // Input 44, 55, 111, 2 Output = "2"
            for (int temp = 0; temp < count; temp++)
            {
                numbers[temp] = int.Parse(Console.ReadLine());
            }

            int smallest = numbers[0];
            for (int small = 1; small < numbers.Length; small++)
            {
                if (smallest > numbers[small])
                {
                    smallest = numbers[small];
                }
            }
            Console.WriteLine("Smallest Number is : \"{0}\"",smallest);
            Console.ReadKey();
        }
    }
}