I get three values from 开发者_Go百科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:
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).
To do this without Linq, you basically just have to mimic the logic performed by the extension method:
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.
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)
Lowest
return (x < y) ? (x < z ? x : z) : (y < z ? y : z)
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;
}
}
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
Using LINQ-to-Objects, you could do something like this.
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.
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);
}
}
Here is simple logic for finding Smallest Number
Input : 11, 0 , 3, 33 Output : "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();
}
}
}
精彩评论