格式化产生随机数

代码

public static void main(String[] args) {
        int count = 0;
        Random random = new Random();
        for (int i=0;i<100;i++){
            count ++;
            //生成0-100的随机整数
            int a = random.nextInt(101);
            //格式化数字
            String b = new DecimalFormat("000").format(a);
            System.out.print(b+" ");

            //每十个数字换行
            if ((count%10) == 0){
                System.out.println("");
            }
        }
    }

输出结果

022 072 086 065 044 003 088 029 087 081 
072 066 044 077 097 050 023 018 078 097 
046 059 070 088 077 021 082 072 086 072 
044 092 075 013 072 087 061 091 020 036 
049 100 015 097 087 082 042 003 008 092 
027 098 057 077 088 030 084 087 036 056 
081 058 028 090 011 063 069 036 089 076 
045 050 017 029 001 018 091 018 063 094 
080 060 026 089 035 054 003 007 063 062 
085 076 051 000 081 048 060 029 034 052 

Process finished with exit code 0

 


版权声明:本文为qq_37782076原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。