Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using a while loop to replace array values

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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!

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


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default 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.

Similar Threads

  1. Loop to find and replace multiple instances of a char
    By Olympaphibian89 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 25th, 2012, 04:11 PM
  2. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  3. TAKE & PUT VALUES INTO AN ORACLE TABLE THRU A LOOP
    By jai in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 17th, 2011, 05:39 AM
  4. Help with accumulating sum values in do while loop
    By steel55677 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 19th, 2011, 11:01 PM
  5. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM