Results 1 to 7 of 7
Thread: Logic Help
- 01-22-2012, 07:16 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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 }
- 01-22-2012, 01:22 PM #2
Re: Logic Help
Can you explain how you want to choose a color? When do you do it randomly and when does the user choose?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.
For example the user gives the program 6 colors and the program randomly chooses one of those six colors.
- 01-22-2012, 07:38 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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.
- 01-22-2012, 07:43 PM #4
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.
- 01-22-2012, 09:44 PM #5
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.
- 01-22-2012, 11:16 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: Logic Help
Thanks for the suggestions you guys, got it working.
-
Re: Logic Help
What is supposed to happen when the random variable, set, is 0 which will happen 1 out of 4 times?
Similar Threads
-
Need help in logic
By nn12 in forum New To JavaReplies: 3Last Post: 03-23-2011, 06:44 PM -
Need help on logic
By nn12 in forum New To JavaReplies: 6Last Post: 03-10-2011, 11:06 AM -
Logic not working
By Prajin in forum AWT / SwingReplies: 1Last Post: 07-19-2010, 07:54 PM -
need a logic for this
By rajivjoshi in forum New To JavaReplies: 4Last Post: 06-12-2010, 02:18 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks