我试图从for循环的结果中创建一个变量.首先是这可能吗?如果有可能我该怎么办?到目前为止这是我的代码:
Random r = new Random();
Random n = new Random();
int p = (int)(n.nextInt(14));
for (int i = 0; i < p; i++) {
char c = (char) (r.nextInt(26) + 'a');
System.out.println(c);}
String word = ("output of forloop goes here");
Run Code Online (Sandbox Code Playgroud)
我想使用for循环将所有随机生成的字母放入一个单词中.我该怎么做呢?
使用StringBuilder:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < p; i++) {
char c = (char) (r.nextInt(26) + 'a');
sb.append(c);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以调用sb.toString()以获取生成的String.
| 归档时间: |
|
| 查看次数: |
1381 次 |
| 最近记录: |