Java 防止一行中出现相同的随机输出
我的代码应该只是问一个问题,但我想防止它问同一个随机问题两次Java 防止一行中出现相同的随机输出,java,Java,我的代码应该只是问一个问题,但我想防止它问同一个随机问题两次 public static String askQuestion(){ if (hasMoreQuestions() == true);{ int oldq = String[] Question = {"How are you today?","Do you enjoy the rain?","Meow?"}; Random randomno = new Random();
public static String askQuestion(){
if (hasMoreQuestions() == true);{
int oldq =
String[] Question = {"How are you today?","Do you enjoy the rain?","Meow?"};
Random randomno = new Random();
int nextq = randomno.nextInt(3);
if (oldq == nextq){
askQuestion();
}
oldq = nextq;
return Question[nextq];
}
}
这是我的尝试,我希望简单地将以前的输出与随机选择的新输出进行比较。但是我是Java新手,我很难找到实现这一点的最佳方法。第一次尝试
您可以尝试检查不同的数字,然后在这些数字索引处打印问题,而不是递归调用相同的函数,例如:
import java.util.Random;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
n1 = r.nextInt(3);
do {
n2 = r.nextInt(3);
} while (n2 == n1);
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
System.out.println(question[n1]);
System.out.println(question[n2]);
}
}
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
HashMap <Integer, String> map = new HashMap<Integer, String>();
String q = null;
for (int i = 0; i < question.length; i++) {
map.put(i, question[i]); //Filling the map
}
do {
n1 = r.nextInt(question.length);
while (!map.containsKey(n1)) {
n1 = r.nextInt(question.length);
}
q = map.get(n1);
System.out.println(q); //Print the question
map.remove(n1); //Remove it from set
} while (map.size() > 0);
}
}
这张照片是:
How are you today?
Do you enjoy the rain?
第二次尝试 另一种方法是使用,其中保留唯一的键,并在打印完元素后删除它们,例如:
import java.util.Random;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
n1 = r.nextInt(3);
do {
n2 = r.nextInt(3);
} while (n2 == n1);
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
System.out.println(question[n1]);
System.out.println(question[n2]);
}
}
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
HashMap <Integer, String> map = new HashMap<Integer, String>();
String q = null;
for (int i = 0; i < question.length; i++) {
map.put(i, question[i]); //Filling the map
}
do {
n1 = r.nextInt(question.length);
while (!map.containsKey(n1)) {
n1 = r.nextInt(question.length);
}
q = map.get(n1);
System.out.println(q); //Print the question
map.remove(n1); //Remove it from set
} while (map.size() > 0);
}
}
第一次尝试
您可以尝试检查不同的数字,然后在这些数字索引处打印问题,而不是递归调用相同的函数,例如:
import java.util.Random;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
n1 = r.nextInt(3);
do {
n2 = r.nextInt(3);
} while (n2 == n1);
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
System.out.println(question[n1]);
System.out.println(question[n2]);
}
}
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
HashMap <Integer, String> map = new HashMap<Integer, String>();
String q = null;
for (int i = 0; i < question.length; i++) {
map.put(i, question[i]); //Filling the map
}
do {
n1 = r.nextInt(question.length);
while (!map.containsKey(n1)) {
n1 = r.nextInt(question.length);
}
q = map.get(n1);
System.out.println(q); //Print the question
map.remove(n1); //Remove it from set
} while (map.size() > 0);
}
}
这张照片是:
How are you today?
Do you enjoy the rain?
第二次尝试 另一种方法是使用,其中保留唯一的键,并在打印完元素后删除它们,例如:
import java.util.Random;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
n1 = r.nextInt(3);
do {
n2 = r.nextInt(3);
} while (n2 == n1);
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
System.out.println(question[n1]);
System.out.println(question[n2]);
}
}
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
public class UniqueRandom {
public static void main(String args[]) {
Random r = new Random();
int n1, n2;
String question[] = {"How are you today?","Do you enjoy the rain?","Meow?"};
HashMap <Integer, String> map = new HashMap<Integer, String>();
String q = null;
for (int i = 0; i < question.length; i++) {
map.put(i, question[i]); //Filling the map
}
do {
n1 = r.nextInt(question.length);
while (!map.containsKey(n1)) {
n1 = r.nextInt(question.length);
}
q = map.get(n1);
System.out.println(q); //Print the question
map.remove(n1); //Remove it from set
} while (map.size() > 0);
}
}
首先,你必须意识到,通过干扰正态分布,你不是在问“随机”问题。废话,废话,纯粹的胡说八道。我们都知道你的意思:你想提出从某个集合中随机抽取的问题,该集合不包括最近提出的问题。 这表明了一种非常简单的方法:从集合中随机抽取一个问题,并将其呈现给用户。将其从集合中删除,并将其存储为
previousQuestion
。在询问下一个问题(这当然成为前面的问题)之后将其恢复到池中,等等。
如果您使用某种类型的集合而不是数组,那么这将更容易实现
但是,如果您热衷于使用阵列,则可以这样做(只添加您不知道的部分):
首先,你必须意识到,通过干扰正态分布,你不是在问“随机”问题。废话,废话,纯粹的胡说八道。我们都知道你的意思:你想提出从某个集合中随机抽取的问题,该集合不包括最近提出的问题。 这表明了一种非常简单的方法:从集合中随机抽取一个问题,并将其呈现给用户。将其从集合中删除,并将其存储为
previousQuestion
。在询问下一个问题(这当然成为前面的问题)之后将其恢复到池中,等等。
如果您使用某种类型的集合而不是数组,那么这将更容易实现
但是,如果您热衷于使用阵列,则可以这样做(只添加您不知道的部分):
这篇文章将帮助你这篇文章将帮助你设置循环中的值,而不会被覆盖或忽略。这是我的心愿。这很有效,谢谢。很高兴我能帮忙。您应该考虑查看SET(),因为它们非常适合于这类问题(并且集合也非常基本,数学上)在循环中设置值而不被它们覆盖或忽略,这是在欺骗我。这很有效,谢谢。很高兴我能帮忙。你应该考虑一下SET(),因为它们非常适合于这类问题(并且集合也是非常基本的,数学上的)。