Using a while loop to replace array values
I'm working on a program that simulates buying a gumball from a gumball machine until you get a red one. So far the program generates a certain amount of gumballs of each color. I gave each gumball color a number (yellow=1, blue=2, etc.) and put them all in an array. Now, I'm trying to change each array value to "999" as they are individually picked from the machine so that the colors won't repeat.
The do/while loop seems to only work once. How do I get it to keep finding random numbers?
Would really appreciate any help/suggestions!
Code Java:
public class gumball {
public static void main(String[] args) {
System.out.println("Welcome to the CIMS Gumball Machine Simulator!");
System.out.println("You are starting with the following Gumballs:");
//determines how many gumballs to start with
int yellow = (int) ((Math.random() * 6) + 10);
System.out.println(yellow + " " + "Yellow");
int blue = (int) ((Math.random() * 10) + 1);
System.out.println(blue + " " + "Blue");
int white = (int) ((Math.random() * 10) + 6);
System.out.println(white + " " + "White");
int green = (int) ((Math.random() * 16) + 10);
System.out.println(green + " " + "Green");
int black = (int) ((Math.random() * 12) + 1);
System.out.println(black + " " + "Black");
int purple = (int) ((Math.random() * 6) + 5);
System.out.println(purple + " " + "Purple");
int silver = (int) ((Math.random() * 3) + 4);
System.out.println(silver + " " + "Silver");
int cyan = (int) ((Math.random() * 8) + 5);
System.out.println(cyan + " " + "Cyan");
int magenta = (int) (Math.random() * 10);
System.out.println(magenta + " " + "Magenta");
int red = 1;
System.out.println(red + " " + "Red");
System.out.println("Here are your random purchases:");
//gumball total
int total = yellow + blue + white + green + black + purple + silver + cyan + magenta + red;
//array of chosen gumballs
int[] Array = new int[total];
int i;
for (i=0; i<yellow; i++){
Array[i]=1;
}
for (i=yellow; i<yellow+blue; i++){
Array[i]=2;
}
for (i=yellow+blue; i<yellow+blue+white; i++){
Array[i]=3;
}
for (i=yellow+blue+white; i<yellow+blue+white+green; i++){
Array[i]=4;
}
for (i=yellow+blue+white+green; i<yellow+blue+white+green+black; i++){
Array[i]=5;
}
for (i=yellow+blue+white+green+black; i<yellow+blue+white+green+black+purple; i++){
Array[i]=6;
}
for (i=yellow+blue+white+green+black+purple; i<yellow+blue+white+green+black+purple+silver; i++){
Array[i]=7;
}
for (i=yellow+blue+white+green+black+purple+silver; i<yellow+blue+white+green+black+purple+silver+cyan; i++){
Array[i]=8;
}
for (i=yellow+blue+white+green+black+purple+silver+cyan; i<yellow+blue+white+green+black+purple+silver+cyan+magenta; i++){
Array[i]=9;
}
for (i=yellow+blue+white+green+black+purple+silver+cyan+magenta; i<total; i++){
Array[i]=10;
}
//part I'm working on
int randomNumber = (int)(Math.random() * total);
while (Array[randomNumber] != 999){
print_gumball(Array[randomNumber]);
Array[randomNumber]=999;
}
}
public static void print_gumball(int x){
switch(x){
case 1: System.out.println("Yellow");
break;
case 2: System.out.println("Blue");
break;
case 3: System.out.println("White");
break;
case 4: System.out.println("Green");
break;
case 5: System.out.println("Black");
break;
case 6: System.out.println("Purple");
break;
case 7: System.out.println("Silver");
break;
case 8: System.out.println("Cyan");
break;
case 9: System.out.println("Magenta");
break;
case 10: System.out.println("Red");
break;
}
}
}
Re: Using a while loop to replace array values
If you want it to keep finding random numbers, you need to place a random number generator within your while loop but the line Array[randomNumber]=999; prevents your loop from re-iterating.
Re: Using a while loop to replace array values
Thanks, but then what would I have to do to change each value to 999 as it's picked?
Re: Using a while loop to replace array values
There are many different ways you can ensure each value is picked only once. One such way could be to change the return type of the print_gumball method from void to any data type. You can have an array that stores the result from the print_gumball method. During each iteration, the new value from print_gumball would be compared to all non-null values stored in the array. If it matches any of them, have the loop continue to the next iteration, such as using the keyword, "continue". If it doesn't match, then store it in the array and have the loop re-iterate. This array and comparison can take place directly in the while loop or you can create a new method for it.