JAVA随机生成四位数的英文验证码,含大小写
时间: 2024-02-25 11:53:46 浏览: 39
好的,以下是生成随机四位数英文验证码的 Java 代码,包含大小写字母:
```java
import java.util.Random;
public class RandomCodeGenerator {
public static void main(String[] args) {
String code = generateCode();
System.out.println("生成的验证码为:" + code);
}
public static String generateCode() {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = random.nextInt(str.length());
sb.append(str.charAt(index));
}
return sb.toString();
}
}
```
以上代码中,`generateCode()` 方法用于生成随机四位数英文验证码,包含大小写字母。`str` 字符串包含所有可能的英文字母,`Random` 类被用于生成随机数,`StringBuilder` 类用于拼接字符串。运行以上代码将输出一个随机生成的四位英文验证码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)