Good day! I’m a beginner in programming and I’ve been working on a program. It uses import java.util.Random because I want my questions to appear randomly in no particular order. But the problem and the only problem is that the question repeats. For an instance, “Are you happy?” is asked three times and “Do you want iPhone 5?” is not even asked. What should I do to just show all the questions in no particular order and so it would not pick the same question many times? So far, this is what I have.
import java.util.Random;
public class RandomQuiz {
public static void main (String args []){
int a, b=0;
String arr [];
arr = new String [5];
a = b;
arr [a] = "Are you happy? \na. yes\t\tb. no\nc. maybe\td. no comment";
a = b+1;
arr [a] = "Did you eat breakfast? \na. yes\t\tb. no\nc. maybe\td. no comment";
a = b+2;
arr [a] = "Have you watched tv? \na. yes\t\tb. no\nc. maybe\td. no comment";
a = b+3;
arr [a] = "Do you want iPhone 5? \na. yes\t\tb. no\nc. maybe\td. no comment";
a = b+4;
arr [a] = "Will you have iPad mini? \na. yes\t\tb. no\nc. maybe\td. no comment";
//prints array values in random
Random randnum = new Random ();
for (int count = 1; count <=5; count++){
a = randnum.nextInt (5);
System.out.println ("Question # " + count + "\n" + arr [a]);
}
}
}
Try this