public class MaoPao { public static void main(String[] args) {
int[] a = new int[] { 4, 2, 1, 3, 5 }; for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1; i++) {
if (a[0] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
System.out.println(a[i]);
}
}}

解决方案 »

  1.   

    public class MaoPao {
    public static void main(String[] args) {
    int[] a = new int[] { 4, 2, 1, 3, 5 }; for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < i; j++) {
    if (a[j] > a[j + 1]) {
    int t = a[j];
    a[j] = a[j + 1];
    a[j + 1] = t;
    }
    }

    }
    for(int s:a){
    System.out.println(s);
    }
    }}
      

  2.   


    public static void test2() {
    int intArray[] = { 10, 5, 85, 40, 20, 78, 66 }; for (int i = intArray.length - 1; i > 0; i--) {
    for (int j = 0; j < i; j++)
    if (intArray[j] > intArray[j + 1]) {
    int t = intArray[j];
    intArray[j] = intArray[j + 1];
    intArray[j + 1] = t;
    }
    } System.out.print(java.util.Arrays.toString(intArray));
    }