用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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短