啥也不说了,直接上图
import java.util.Random;
public class CollectionsSort {
public static char getRandomChar(){
String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Random r = new Random(System.currentTimeMillis());
char ch = str.charAt(r.nextInt(62));
return ch;
}
public static String getRandomString(int length){
StringBuffer str = new StringBuffer();
while(length > 0){
char ch = getRandomChar();
str.append(ch);
length--;
}
return str.toString();
}
public static void main(String[] args) {
String str = getRandomString(10);
System.out.print(str);
}
}
调试运行结果和预计的结果一致,可是直接运行的话,就会出现全是相同的字符。