如何找到最高和最低的数字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; }
我可以计算构造函数中的最高和最低数字吗?
强制性的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; }
同样,为了使其万无一失,您应检查空或空输入数组,因为调用Lowest()将抛出ArrayIndexOutOfBoundsException。
这是一种方法:
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();
最高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)
return (x < y) ? (x < z ? x : z) : (y < z ? y : z)
如果您想简单地检查哪个是最高的,您可以这样做
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
这是你可以做的事情:
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; } }
使用LINQ-to-Objects,你可以做这样的事情。
var numbers = new [] {_score1, _score2, _score3}; numbers.Sort(); var lowest = numbers.First(); var highest = numbers.Last();
供参考:在某些情况下,您将拥有三个以上的变量(可能不知道有多少)。 如果它们存储在数组中,请按以下步骤操作:
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数组。
找到最大和最小的数字
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); } }
这是找到最小数字的简单逻辑
输入:11,0,3,33输出:“0”
上述就是C#学习教程:如何找到最高和最低的数字C#分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
namespace PurushLogics { class Purush_SmallestNumber { static void Main() { int count = 0; Console.WriteLine("Enter Total Number of Integersn"); 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(); } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1031456.html