is there any way to pick a random string from an array, and not pick the string again. once all the strings are picked only once, the program ends.
here is my current system, public static void main (String [] argv) {
//String [] names = new String[5];
String [] names = {"Snoopy", "Donald Duck", "Mickey Mouse", "Little Timmy", "Spiderman"};
//put a list of names in an array
//randomly pick a name out of the array
//how to: randomly pick a number as the index
//range: 0 to the number of names - 1
int n = -1;
int previous = n;
int storing1 = -1;
int storing2 = -1;
int storing3 = -1;
int storing4 = -1;
System.out.println(previous);
while (true) {
System.out.println("Hit \"enter\" to spin");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
//if (s.equalsIgnoreCase("s")) {
storing4 = storing3;
storing3 = storing2;
storing2 = storing1;
storing1 = previous;
previous = n;
n = (int) (Math.random() * names.length);
if (n == previous || n == storing1 || n == storing2 || n == storing3 || n == storing4) {
System.out.println("This person has already been picked");
break;
}
System.out.print("n = " + n + " ");
String picked = names[n];
System.out.println("Congratulations, " + picked + " was picked!");
}
System.out.println(previous);
System.out.println(storing1);
System.out.println(storing2);
System.out.println(storing3);
System.out.println(storing4); //for my own use, to tell if the storing system works properly
}
my problem is that the console will return "this person has already been picked" and on and on. not to say that the program ending system doesn't work since the break will work prematurely.
[1]: https://i.sstatic.net/54Eef.png [as you can see, the program ends even though not all five names has been picked because it has picked the same one before][1] sorry if this seems simple, I just started java a few months ago. i don't think I am allowed to use ArrayList and that's been the only solution to this I've found so far. thank you.