0

I created a quote generator and when i click on a person's name, it prints out a random quote of theirs. Now the person has 20 quotes all stored in its own array. How do I get it to print out random quotes tat have not been used, once tehy all have been used, it can repeat.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

A very simplistic way to do it is to have 2 arrays; one to hold the quotes, the second of the same size to indicate whether a quote has been used or not. So, you might initially fill the second array, which indicates whether a quote has been used, with zeros. Let's call this array "indicator". When a quote from the first array (call it quotes) is used, you change the same position in the indicator array to 1. If your random number lands on an array element set to 1 in indicators, you ignore the quote and get another random number. When all the indicator elements are 1, you start over by setting them all to zero again. So, think of how you might use a loop to accomplish this, and some other piece of programming to examine the indicator array for all ones, at which time this other piece resets all elements to zero.

There are many, many ways to accomplish what you want, this is just one way that popped into mind.

0

That's a perfectly reasonable way to do it, but this being Java please use a boolean array, not a numeric array with 0/1 values.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.