c# 数字中找出最大值
Given three integer numbers and we have to find largest number using C# program.
给定三个整数,我们必须使用C#程序找到最大的数字。
查找三个数字中的最大值 (Finding largest of three numbers)
To find the largest number from given three numbers – we will compare their values using either the simple if-else statement or ternary operator. If the first number is greater than the second number and third number, assign the first number to the variable in which we will store the largest number, if the second number is greater than the second number and the third number then assign the second number to the variable, else assign the third number to the variable.
要从给定的三个数字中找到最大的数字 –我们将使用简单的if-else语句或三元运算符比较它们的值。 如果第一个数字大于第二个数字和第三个数字,则将第一个数字分配给我们将在其中存储最大数字的变量;如果第二个数字大于第二个数字和第三个数字,则将第二个数字分配给变量,否则将第三个数字分配给变量。
Let suppose given numbers are a, b and c and we have to store the largest number in large.
假设给定的数字是a , b和c ,我们必须将大的数字存储为大 。
Syntax using if-else
使用if-else的语法
if (a > b && a > c)
large = a;
else if (b > a && b > c)
large = b;
else
large = c;
Syntax using ternary operator
使用三元运算符的语法
large = (a > b && a > c) ? a : (b > a && b > c) ? b : c;
C#代码查找三个数字中的最大值 (C# code to find largest of three numbers)
Here, we are asking for three integer numbers from the user and finding the largest one using if-else and ternary operator.
在这里,我们要求用户提供三个整数,并使用if-else和三进制运算符找到最大的整数。
// C# program to find largest of three numbers
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
int a;
int b;
int c;
int large;
//input the numbers
Console.Write("Enter first number : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter third number : ");
c = Convert.ToInt32(Console.ReadLine());
//finding largest number using if-else
if (a > b && a > c)
large = a;
else if (b > a && b > c)
large = b;
else large = c;
//printing
Console.WriteLine("Using if-else...");
Console.WriteLine("Largest number is {0}", large);
//finding largest number using ternary operator
large = (a > b && a > c) ? a : (b > a && b > c) ? b : c;
//printing
Console.WriteLine("Using ternary operator...");
Console.WriteLine("Largest number is {0}", large);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
输出量
First run:
Enter first number : 10
Enter second number: 20
Enter third number : 30
Using if-else...
Largest number is 30
Using ternary operator...
Largest number is 30
Second run:
Enter first number : 10
Enter second number: 30
Enter third number : 20
Using if-else...
Largest number is 30
Using ternary operator...
Largest number is 30
Third run:
Enter first number : 30
Enter second number: 20
Enter third number : 10
Using if-else...
Largest number is 30
Using ternary operator...
Largest number is 30
Fourth run:
Enter first number : 10
Enter second number: 20
Enter third number : 20
Using if-else...
Largest number is 20
Using ternary operator...
Largest number is 20
翻译自: https://www.includehelp.com/dot-net/find-largest-of-three-numbers-in-c-sharp.aspx
c# 数字中找出最大值