如何从Java上的数组中调用随机字符串?

时间:2016-10-05 23:12:26

标签: java

String[] prompts = {"Describe to me in a sentence why this is a cool program.", 
                    "Describe to me in a sentence how your day was.", 
                    "Describe to me in a sentence what programming means to you.", 
                    "Describe to me in a sentence why food is neccessary for humans."};
System.out.println(prompts);

我想从数组中调用一个随机字符串,我应该如何报告它?

1 个答案:

答案 0 :(得分:2)

只需要在数组长度范围内随机生成一个索引

int i = new Random().nextInt(prompts.length);
System.out.println(prompts[i]);