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!
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; } } }