数组元素从小到大排列,每次循环把最小的元素提取到最前面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 45, 62, 32, 11, 37, 31 };
int j, temp;
for (int i = 0; i < arr.Length - 1; i++)
{
j = i + 1;
ID:
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
goto ID;
}
else
if (j < arr.Length - 1)
{
j++;
goto ID;
}
}
foreach (int n in arr)
{
Console.Write(n + " ");
}
}
}
}
虽然goto语句有一定的使用价值,但是目前对它的使用存在争议。有人建议避免使用它,有人建议把它用来作为排除错误的基本工具,各种观点截然不同,虽然许多人不用goto语句也能够编程,但是仍然有人使用它。所以要小心使用,同时一定要确保程序是可以维护的。