代码
方法一:
public class RandomTest extends TestCase {
public void testRandom1() throws Exception {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] c = s.toCharArray();
Random random = new Random();
for( int i = 0; i < 8; i ++) {
System.out.println(c[random.nextInt(c.length)]);
}
}
}
方法二:
public void testRandom2() throws Exception {
Random random = new Random();
for( int i = 0; i < 8; i ++) {
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写还是小写
System.out.println((char)(choice + random.nextInt(26)));
}
}
public class RandomTest extends TestCase {
public void testRandom1() throws Exception {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] c = s.toCharArray();
Random random = new Random();
for( int i = 0; i < 8; i ++) {
System.out.println(c[random.nextInt(c.length)]);
}
}
}
方法二:
public void testRandom2() throws Exception {
Random random = new Random();
for( int i = 0; i < 8; i ++) {
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写还是小写
System.out.println((char)(choice + random.nextInt(26)));
}
}