Java 语言基础

如何产生10个100之内的随机整数输出,并把这10个数按从小到大的顺序输出。

代码如下:

public class Main {

    public static void main(String args[]) {

        int math[] = new int[10];

        for (int i = 0; i < 10; i++) {

            math[i] = (int) (Math.random() * 100);

            System.out.print(math[i] + " ");

        }

        System.out.println();

        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 10; j++) {

                if (math[i] < math[j]) {

                    int l = math[i];

                    math[i] = math[j];

                    math[j] = l;

                }

            }

        }

        for (int i = 0; i < 10; i++) {

            System.out.print(math[i] + " ");

        }

    }

}

运行结果:

 

posted on 2019-06-20 10:12  syz16  阅读(125)  评论(0编辑  收藏  举报

导航