java 中随机应用 已知数组 怎么从中随机抽几个元素出来 组成一句话

这句话不需要读的通

第1个回答  2011-05-18
import java.util.Random;

public class Test {
public static void main(String[] args) {

String str[] ={"java","中随机应用","已知数组","怎么从中随机抽几个元素出来","组成一句话 "};
String str1[] = new String[str.length];
Random r = new Random();
int a=0;
for(int i=0;i<str.length;i++){
str1[a]= str[r.nextInt(5)];
System.out.print(str1[a]+" ");
a++;
}
}
}本回答被提问者采纳
第2个回答  2011-05-18
用随机数方法.获取不超过数组长度-1 的随机数.然后取出来.再把字符串加起来..OK
第3个回答  2011-05-16
组成的话需要读得通否?追问

不需要读的通

追答

String strs[];
....
i = new Random().nextInt() % strs.length;
strs[i]...
....

相似回答