日期:2014-05-20 浏览次数:20847 次
int[] numbers = new int[]{3,34,12,33,1,23,12}; for(int i = 0; i < numbers.Length; i++) { for(int j = 0; j < numbers.Length;j++) { if(numbers[i] < numbers[j]) { numbers[i] = numbers[i] + numbers[j]; numbers[j] = numbers[i] - numbers[j]; numbers[i] = numbers[i] - numbers[j]; } } }
------解决方案--------------------
public static void ExecuteSortedMethod(int[] num, int sortedMethod)
{
if (sortedMethod == 1) //升序排列
{
for (int i = 0; i < num.Length -1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
if (sortedMethod == 2) //降序排列
{
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] < num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
}
}
------解决方案--------------------
楼上把sortedMethod变成一个枚举会更好。