Results 1 to 10 of 10
Thread: array with specific needs
- 06-20-2011, 11:40 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
- 06-20-2011, 11:48 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
It doesn't work that way...at what point are you having a problem? Do you know how to create a random value? Do you know how to create an array of said length? Write some code and ask a specific question about the code, this will result in more informative feedbackPlease include an example code.
- 06-20-2011, 11:50 PM #3
What have you tried so far?
Start with a small part of the exercise and add to it. For example can you generate random numbers in the range 0-7?
Then add an array of counters to keep track of how many of each number you have used.
BTW We normally don't provide code until you've shown your efforts.
- 06-21-2011, 01:05 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Yeah buddy. This is a community and they're super helpful, but you do have to show that you attempted something. This isn't www.domyJavaprogrammingforme.com .
As Norm said, start with the basics. If you can describe what you want your code to do, then try doing each individual steps. For the random numbers, you can use the method Math.random(). If you read the API, it will tell you that it only generates numbers from 0.0 to 1.0 (excluding 1.0), so you can mutilply it by 10 and cast it as an integer. You might need to write a conditional to verify that only numbers that are less or equal to 7 are returned, and so on and so forth. I am fairly new as well and it helps to really think about your coding with small steps, projects and effort.
- 06-21-2011, 01:30 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
This is my program
int[] imgNum = new int [16];
int randomNum;
int appear = 0;
int flag;
/*for (int i = 0 ; i < imgNum.length ; i++)
{
for (int j = 0 ; j < imgNum.length ; j++)
{
randomNum = (int) (Math.random () * 7);
while (flagExit == 0)
{
}
} //tests each value of the array against the random number
} //goes through the array storing each value
for (int i = 0 ; i < imgNum.length ; i++)
{
c.println (imgNum [i]);
}
- 06-21-2011, 01:41 AM #6
Have you printed out some of the random numbers that are generated to be sure that they are in the desired range?
Now write code to store the first 16 random numbers in the array.
An easy way to see the contents of the array is to use the Arrays.toString() method. It will print out the whole array on one line.
After that works, then we'll work on getting a max of two of the same numbers.
- 06-21-2011, 01:50 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
Okay now i have this i thought it would work but it doesn't:
PHP Code:for (int i = 0 ; i < imgNum.length ; i++) { imgNum [i] = (int) (Math.random () * 7); for (int j = 0 ; j < imgNum.length ; j++) { if (imgNum [i] == 0) { count [0]++; } else if (imgNum [i] == 1) { count [1]++; } else if (imgNum [i] == 2) { count [2]++; } else if (imgNum [i] == 3) { count [3]++; } else if (imgNum [i] == 4) { count [4]++; } else if (imgNum [i] == 5) { count [5]++; } else if (imgNum [i] == 6) { count [6]++; } else if (imgNum [i] == 7) { count [7]++; } else { // do nothing } if (count [0] > 2 || count [1] > 2 || count [2] > 2 || count [3] > 2 || count [4] > 2 || count [5] > 2 || count [6] > 2 || count [7] > 2) { imgNum [i] = (int) (Math.random () * 7); } } } // end for
- 06-21-2011, 02:49 AM #8
Can you explain your logic for the code you posted?
Do you see a pattern in the code you posted?
The orange could replace the red as the array index:Java Code:[COLOR="orange"]imgNum [i][/COLOR] ==[COLOR="red"] 0[/COLOR]) { count [COLOR="red"][0[/COLOR]]++;
count[imgNum[i]]++;Last edited by Norm; 06-21-2011 at 02:52 AM.
- 06-21-2011, 03:04 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
PHP Code:int[] count = new int [8]; // array that makes sure there arent more than 2 of the same number int[] imgNum = new int [16]; // array numbers stored in int randomNum; for (int i = 0 ; i < count.length ; i++) // make sure count = 0 at startup { count [i] = 0; } for (int i = 0 ; i < imgNum.length ; i++) { randomNum = (int) (Math.random () * 7); // make random number if (randomNum == 0)// find value of number and at the count of how many times thats been on the screen { count [0]++; } else if (randomNum == 1) { count [1]++; } else if (randomNum == 2) { count [2]++; } else if (randomNum == 3) { count [3]++; } else if (randomNum == 4) { count [4]++; } else if (randomNum == 5) { count [5]++; } else if (randomNum == 6) { count [6]++; } else if (randomNum == 7) { count [7]++; } else { // do nothing } if (count [0] > 2 || count [1] > 2 || count [2] > 2 || count [3] > 2 || count [4] > 2 || count [5] > 2 || count [6] > 2 || count [7] > 2)//if this applies the number cant go in the array cause its the 3rd of its kind { i--; // to make the loop repeat itself count [randomNum]--; // to make sure the count of how many times that number has been there is set back to 2 } else if (count [0] <= 2 || count [1] <= 2 || count [2] <= 2 || count [3] <= 2 || count [4] <= 2 || count [5] <= 2 || count [6] <= 2 || count [7] <= 2)// make the radnom number go into the array { imgNum [i] = randomNum; } } // end for i for (int i = 0 ; i < imgNum.length ; i++) { c.println (imgNum [i]); // test to work it doesnt yet }
- 06-21-2011, 03:14 AM #10
Your code has too many repeated almost identical lines. When you see a pattern like this you shoud try to collapse them into a few lines.
You documented your code but didn't really explain your logic.
Maybe something like this:
Get a random number range 0-7.
Check if we have 2 of those numbers -> here you use the random number as an index to the count array
If we do, go back and get another random number
if not, put the number in the output array and increment the array's index and add one to the count array
if more needed, go back to Get a random number
Similar Threads
-
How to allow specific letters only?
By ctmarco3 in forum New To JavaReplies: 7Last Post: 04-01-2011, 04:41 AM -
Getting specific hash in a 2D-Array
By benn22 in forum New To JavaReplies: 0Last Post: 03-18-2011, 07:24 AM -
How to: Select specific array indices?
By louist in forum New To JavaReplies: 3Last Post: 03-07-2011, 10:21 PM -
Specific Functions for URL's
By lyric0n in forum Java ServletReplies: 3Last Post: 04-06-2010, 12:34 PM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks