Results 1 to 3 of 3
Thread: Java Arrays & Loops
- 10-14-2011, 04:39 PM #1
Java Arrays & Loops
Hi guys, I'm having a bit of trouble with the basics, I need to generate four unique numbers between 1 and 10 for each line. It all works fine except i cant work out how to check if the number is unique??
thanks in advance for any help you can give!AdamJava Code:linesArray = new int[lines][4]; Random diceRoller = new Random(); //for each of the lines requested for(int i = 0; i < lines; i++) { //for each number in the line int j = 0; while(j < 4){ //generate a random number int roll = diceRoller.nextInt(10) + 1; if(linesArray[i][0] == roll || linesArray[i][1] == roll || linesArray[i][2] == roll || linesArray[i][3] == roll) { return; } else { linesArray[i][j] = roll; j++; } } }
- 10-14-2011, 04:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Arrays & Loops
Store the ten numbers [1 ... 10] in a Collection, call Collections.shuffle( ... ) and get the first four numbers of that Collections.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-14-2011, 05:14 PM #3
Similar Threads
-
Urgent! Java Assignment with loops in Arrays
By Xeher in forum New To JavaReplies: 4Last Post: 11-28-2010, 12:49 AM -
How to use arrays and loops?
By asadzarrar in forum New To JavaReplies: 1Last Post: 10-31-2010, 11:04 PM -
Printing Two Dimensional Arrays with for loops
By mcnam4119 in forum JCreatorReplies: 3Last Post: 10-06-2010, 05:27 AM -
A few questions about arrays and loops
By Jamison5213 in forum New To JavaReplies: 1Last Post: 12-22-2009, 05:59 AM -
Some problems with arrays and loops
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 12:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks