List<String> n = new ArrayList<String>();
n.add("Allen");
n.add("beer");
n.add("Johnson");
n.add("bar");
n.add("girl");
List<String> v = new ArrayList<String>();
v.add("shot");
v.add("drank");
v.add("ate");
v.add("hit");
v.add("tossed");
int noun = (int)(Math.random() * n.size());
int verb = (int)(Math.random() * v.size());
int noun2 = (int)(Math.random() * n.size());
int verb2 = (int)(Math.random() * v.size());
int noun3 = (int)(Math.random() * n.size());
String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3));
int x = 0;
while (x < 5) {
System.out.println(a);
x++;
}
然后继续只重复一个陈述,例如:
约翰逊扔了酒吧并击中了酒吧
约翰逊扔了酒吧并击中了酒吧
约翰逊扔了酒吧并击中了酒吧
约翰逊扔了酒吧并击中了酒吧
约翰逊扔了酒吧并击中了酒吧
如何随机使其随机变化。
答案 0 :(得分:1)
您只运行一次字符串赋值。所以它总是一样的。完成作业:
String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3));
在循环内部,如果你想要多次重新生成它,就像这样:
int x = 0;
while (x < 5) {
int noun = (int)(Math.random() * n.size());
int verb = (int)(Math.random() * v.size());
int noun2 = (int)(Math.random() * n.size());
int verb2 = (int)(Math.random() * v.size());
int noun3 = (int)(Math.random() * n.size());
String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3));
System.out.println(a);
x++;
}
答案 1 :(得分:1)
您需要重新生成随机字符串,而不是使用一个字符串。只需将随机逻辑放入while循环即可。你可以尝试:
List<String> n = new ArrayList<String>();
n.add("Allen");
n.add("beer");
n.add("Johnson");
n.add("bar");
n.add("girl");
List<String> v = new ArrayList<String>();
v.add("shot");
v.add("drank");
v.add("ate");
v.add("hit");
v.add("tossed");
int x = 0;
while (x < 5) {
int noun = (int)(Math.random() * n.size());
int verb = (int)(Math.random() * v.size());
int noun2 = (int)(Math.random() * n.size());
int verb2 = (int)(Math.random() * v.size());
int noun3 = (int)(Math.random() * n.size());
String a = n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3);
System.out.println(a);
x++;
}
答案 2 :(得分:1)
你需要&#34;重新随机化&#34;你的随机变量每个都通过while循环运行。即使您使用Math.random()
声明变量,它也不会在每次使用时都发生变化。声明变量后,除非通过赋予变量值来更改变量,否则它不会发生变化。所以,你想要这样做:
int x = 0;
while (x < 5) {
int noun = (int)(Math.random() * n.size());
int verb = (int)(Math.random() * v.size());
int noun2 = (int)(Math.random() * n.size());
int verb2 = (int)(Math.random() * v.size());
int noun3 = (int)(Math.random() * n.size());
String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3));
System.out.println(a);
x++;
}
这样,您可以在while循环的每次迭代中更改noun
,verb
等的值。
答案 3 :(得分:1)
您正在设置&#34;随机&#34;变量在进入while循环之前。因此,对于while循环的每次迭代,您使用相同的值。为了在每次迭代中获得不同的结果,您还需要在while循环中包含随机数生成。
int x = 0;
while (x < 5) {
int noun = (int)(Math.random() * n.size());
int verb = (int)(Math.random() * v.size());
int noun2 = (int)(Math.random() * n.size());
int verb2 = (int)(Math.random() * v.size());
int noun3 = (int)(Math.random() * n.size());
String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3));
System.out.println(a);
x++;
}