用Java生成随机字符串
让我们首先声明一个字符串数组并初始化-
String[] strArr = { "P", "Q", "R", "S","T", "U", "V", "W" };现在,创建一个Random对象-
Random rand = new Random();
生成随机字符串-
int res = rand.nextInt(strArr.length);
示例
import java.util.Random;
public class Demo {
public static void main(String[] args) {
String[] strArr = { "P", "Q", "R", "S","T", "U", "V", "W" };
Random rand = new Random();
int res = rand.nextInt(strArr.length);
System.out.println("Displaying a random string = " + strArr[res]);
}
}输出结果
Displaying a random string = R
让我们再次运行它以获得不同的随机字符串-
Displaying a random string = Q
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿