从这里选择一个字符串数组的随机元素? [英] Pick a random element from a string array?

查看:147
本文介绍了从这里选择一个字符串数组的随机元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这有code,这(我希望)从66字的文本文件中读取并把单词放到一个数组。

I've got this code, which (I hope) reads from a text file with 66 words and puts the words into an array.

BufferedReader buff = null;
String wordlist=new String[66];
int i=0;

try {
    buff = new BufferedReader(new FileReader("C:\\easy.txt"));
    wordlist[i] = buff.readLine();
    while(wordlist[i] != null&i<66){
        wordlist[i]=buff.readLine();
        i++;
    }
}

我要挑从阵列中随机字。不过尝试了几件事情我自己,看着其他问题似乎并没有工作。任何帮助将是AP preciated

I want to pick a random word from the array. However trying a few things myself and looking at other questions doesn't seem to work. Any help would be appreciated

推荐答案

最简单的code恕我直言将是:

The simplest code IMHO would be:

String word = wordlist[new Random().nextInt(wordlist.length)];

这篇关于从这里选择一个字符串数组的随机元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆