int temp;
if (x > y) { temp = x; x = y; y = temp; }
if (x > z) { temp = x; x = z; z = temp; }
if (y > z) { temp = z; y = z; z = temp; }
然后依次输出X Y Z
或
if(a>b)
{
if (b > c)
{
Console.WriteLine("{0}{1}{2}",a,b,c);
}
}
else if(a>c)
{
if(c>b)
{
Console.WriteLine("{0}{1}{2}",a,c,b);
}
}
else if(b>c)
{
if(c>a)
{
Console.WriteLine("{0}{1}{2}",b,c,a);
}
}
或通用算法
冒泡排序:
static void Main(string[] args)
{
int[] array = new int[6];
Console.WriteLine("请输入6个整形数字");
for (int i = 0; i < array.Length;i++ ) {
array[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < array.Length - 1;i++ ) {
for (int j = 0; j < array.Length - i - 1;j++ )
{
if(array[j]>array[j+1]){
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
Console.WriteLine("排序之后");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
}