题目

1、一个数组,求得出的最大数字

import java.util.Arrays;

public class Main1 {

    public static void main(String[] args) {
        int[] a = new int[] { 89,78};
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (compare(a[j], a[j + 1])) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));

    }

    private static boolean compare(int a, int b) {// 比较大小,前大为true
        String s1 = a + "" + b;
        String s2 = b + "" + a;

        int result = s1.compareTo(s2);
        if (result < 0) {
            return true;
        }

        return false;
    }

}

 

posted @ 2020-09-13 18:22  我们村里的小花儿  阅读(106)  评论(0编辑  收藏  举报