如何使用 C# 查找两个数字之间的最大值?
csharpprogrammingserver side programming更新于 2025/5/19 18:22:17
首先,声明并初始化两个数字。
int num1 = 50; int num2 = 90;
然后,使用 if-else 查找最大值。
if (num1 > num2) {
maxNum = num1;
} else {
maxNum = num2;
}
上面,我们将最大值设置为变量 maxNum 并在稍后打印它。
以下是在 C# 中查找两个数字之间的最大值的完整示例。
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int num1 = 50;
int num2 = 90;
int maxNum;
Console.WriteLine("Number 1: "+num1);
Console.WriteLine("Number 2: "+num2);
if (num1 > num2) {
maxNum = num1;
} else {
maxNum = num2;
}
Console.WriteLine("Maximum number is: "+maxNum);
Console.ReadKey() ;
}
}
}
输出
Number 1: 50 Number 2: 90 Maximum number is: 90

