网上的方法
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))); } }