简体   繁体   English

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

[英]How to find the highest and the lowest number C#

I get three values from three variables. 我从三个变量得到三个值。 How can i check who is the highest number and who is the lowest number? 如何查看谁是最高号码谁是最低号码?

The numbers are represented like this: 数字表示如下:

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

Code: 码:

Public int Highest
{
  return the highest number here;
}

public int Lowest
{
  return the lowest number here;
}

Can i calculate the highest and the lowest number in my constructor? 我可以计算构造函数中的最高和最低数字吗?

The obligatory Linq answer: 强制性的Linq回答:

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

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

The beauty of this one is that it can take any number of integer inputs. 这个的美妙之处在于它可以采用任意数量的整数输入。 To make it fail-safe you should check for a null/empty inputs array (meaning nothing was passed into the method). 为了使其成为故障安全,您应该检查null / empty输入数组(意味着没有任何内容传递给该方法)。

To do this without Linq, you basically just have to mimic the logic performed by the extension method: 要在没有Linq的情况下执行此操作,您基本上只需要模仿扩展方法执行的逻辑:

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

Again, to make it foolproof you should check for an empty or null inputs array, because calling Lowest() will throw an ArrayIndexOutOfBoundsException. 再次,为了使其万无一失,您应检查空或空输入数组,因为调用Lowest()将抛出ArrayIndexOutOfBoundsException。

This is one way to do it: 这是一种方法:

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

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

Highest return (x > y) ? (x > z ? x : z) : (y > z ? y : z) 最高return (x > y) ? (x > z ? x : z) : (y > z ? y : z) return (x > y) ? (x > z ? x : z) : (y > z ? y : z)

Lowest return (x < y) ? (x < z ? x : z) : (y < z ? y : z) 最低return (x < y) ? (x < z ? x : z) : (y < z ? y : z) return (x < y) ? (x < z ? x : z) : (y < z ? y : z)

If you want to simply check which is the highest you can do this 如果您想简单地检查哪个是最高的,您可以这样做

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

Similarly, you can find the lowest like so 同样,你可以找到最低价

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

Here's something you could do: 这是你可以做的事情:

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

Find the Largest and smallest number 找到最大和最小的数字

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

Using LINQ-to-Objects, you could do something like this. 使用LINQ-to-Objects,你可以做这样的事情。

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

For a reference: in some cases you'll be having more than three variables (possibly not knowing how many). 作为参考:在某些情况下,您将拥有三个以上的变量(可能不知道有多少)。 If they are stored in an array, here's the way to do it: 如果它们存储在一个数组中,这就是这样做的方法:

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

This will work for any length of int array. 这适用于任何长度的int数组。

Here is simple logic for finding Smallest Number 这是找到最小数字的简单逻辑

Input : 11, 0 , 3, 33 Output : "0" 输入:11,0,3,33输出:“0”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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