本文共 660 字,大约阅读时间需要 2 分钟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ArrayRandomData { char[] datas ; public ArrayRandomData() { String str = "abcdefghijklmnopqrstuvwxyz1234567890" ; datas = str.toCharArray(); } public String getRandomData() { char[] rands = new char[ 4 ]; int index; for ( int i= 0 ;i< 4 ;i++) { index = ( int )(Math.random()*datas.length); rands[i] = datas[index]; } return new String (rands); } public static void main( String [] args) { ArrayRandomData ard = new ArrayRandomData(); String randomData = ard.getRandomData(); System.out.println( "随机数是:" +randomData); } } |