C# 代码实现三个整数从小到大排序
以下是使用 C# 代码实现将三个输入的整数从小到大排序输出的示例:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine('请输入三个整数:');
int x = int.Parse(Console.ReadLine());
int y = int.Parse(Console.ReadLine());
int z = int.Parse(Console.ReadLine());
int temp; // 定义一个临时变量
// 排序
if (x > y)
{
temp = x;
x = y;
y = temp;
}
if (x > z)
{
temp = x;
x = z;
z = temp;
}
if (y > z)
{
temp = y;
y = z;
z = temp;
}
Console.WriteLine('排序后的结果为:{0} {1} {2}', x, y, z);
}
}
运行程序后,输入三个整数,程序会将它们从小到大排序并输出。
原文地址: https://www.cveoy.top/t/topic/lOwJ 著作权归作者所有。请勿转载和采集!