currently making a program that will generate a list of Strings from two different arrays, but right now all it does is repeatedly generate Strings the desired amount of times, sometimes with multiple duplicates.
public class ListGenerator
{
public static void main(String[] args)
{
//number generator to determine which string to print
Random generator = new Random();
int rand;
//counter to determine number of printed lines
int counter = 0;
//coin flip to say which array the string will come from
Random coinFlip = new Random();
int coin;
String[] list1;
list1 = new String[5]
list1[0] = "Alpha"
list1[1] = "Beta"
list1[2] = "Charlie"
list1[3] = "Delta"
list1[4] = "Echo"
String[] list2;
list2 = new String[5]
list2[0] = "Apple"
list2[0] = "Pear"
list2[0] = "Grape"
list2[0] = "Banana"
list2[0] = "Orange"
for(counter = 0; counter < 15; counter++)
{
coin = coinFlip.nextInt(2)+1;
if(coin == 1)
{
rand = generator.nextInt(list1.length);
System.out.println(list1[rand]);
}
else if(coin == 2)
{
rand = generator.nextInt(list2.length);
System.out.println(list2[rand]);
}
}
}
}
Is there a way to make it so that I could generate a String like "Apple" or "Beta" no more than twice in the 15 lines generated?
Ex Desired Output:
(1) Apple [printed first time]
(2) Charlie [printed first time]
(3) Pear [printed first time]
(4) Beta [printed first time]
(5) Apple [printed second time]
(6) Echo [printed first time]
(7) Banana [printed first time]
(8) Banana [printed second time]
(9) Echo [printed second time]
(10) Alpha [printed first time]
(11) Grape [printed first time]
(12) Delta [printed first time]
(13) Beta [printed second time]
(14) Orange [printed first time]
(15) Grape [printed second time]
Where 5 of those Strings were generated twice but not more than twice, but instead in my code it could generate any of those Strings 3, 4, 5, etc. times.
I know my code isn't organized the best or the most efficient I just need help on this duplication thing
Aucun commentaire:
Enregistrer un commentaire