Results 1 to 7 of 7

Thread: Logic Help

  1. #1
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Logic Help

    I'm trying to make the program randomly choose between colors, but I also want the user to be able to select which colors it chooses from. I've been using a simple (int)Math.random()*4 to pick a random number assigned to a color, but when the user chooses to omit colors and there are gaps where no color can be assigned because the random number generated is no longer being used, it messes with the program. What I would like to know is, is there a better way of doing this while still using random or is there a better command that will fix the problem?

    The bit of code in question:
    Java Code:
    set = (int)(Math.random()*4);
    
    	if(set == 3 && activeRed == 1){
    		wColor = new Color(256,0,0);//red
    	}
    
    	if(set == 2 && activeGreen == 1){
    		wColor = new Color(0,256,0);//green
    	}
    
    	if(set == 1 && activeBlue == 1){
    		wColor = new Color(0,0,256);//blue
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Logic Help

    I'm trying to make the program randomly choose between colors, but I also want the user to be able to select which colors it chooses from.
    Can you explain how you want to choose a color? When do you do it randomly and when does the user choose?

    For example the user gives the program 6 colors and the program randomly chooses one of those six colors.

  3. #3
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Logic Help

    It is set at choosing from red green and blue, but the user can choose to make it pick from any combination of the three. For example, user wants it to only choose between red and blue while completely ignoring green. The problem is that right now it can still get the number for green, meaning it picks no color at all.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Logic Help

    If the user wants a choice of two colors, then the random choice should only be between those two colors.
    Make a list of the user's selections and chose from that list.

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Logic Help

    You could also make it that if the color that's not wanted is chosen, the randomizer would simply randomize again. Example, the three colours are red, blue and green, but the user only wants the program choose red and blue. The program will begin randomizing. If the colour chosen is green, it will simply randomize again, and keep doing so until it is not green.

  6. #6
    Barbados is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Logic Help

    Thanks for the suggestions you guys, got it working.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 07:44 PM
  2. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 12:06 PM
  3. Logic not working
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2010, 08:54 PM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 03:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •