[英]How to find the highest and the lowest number 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;
}
}
找到最大和最小的數字
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);
}
}
使用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數組。
這是找到最小數字的簡單邏輯
輸入: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.