java自动生层2到20位的字符串账号
时间: 2024-01-26 18:02:33 浏览: 54
以下是Java代码示例,可以生成2到20位的随机字符串账号:
```java
import java.util.Random;
public class RandomStringGenerator {
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
public static String generateRandomString(int length) {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(CHARACTERS.length());
char randomChar = CHARACTERS.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}
public static void main(String[] args) {
for (int i = 2; i <= 20; i++) {
System.out.println("Random string with length " + i + ": " + generateRandomString(i));
}
}
}
```
该代码中使用了一个包含大小写字母和数字的字符集,根据指定长度生成随机字符串。使用循环语句可以生成2到20位长度的随机字符串账号。
阅读全文