Results 1 to 3 of 3
  1. #1
    AdamG's Avatar
    AdamG is offline Member
    Join Date
    Jan 2011
    Location
    Ireland
    Posts
    4
    Rep Power
    0

    Default 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??
    Java 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++;                
    }            
    }        
    }
    thanks in advance for any help you can give!Adam

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    AdamG's Avatar
    AdamG is offline Member
    Join Date
    Jan 2011
    Location
    Ireland
    Posts
    4
    Rep Power
    0

    Default Re: Java Arrays & Loops

    Got it sorted, thanks so much for your help.
    Last edited by AdamG; 10-14-2011 at 05:36 PM.

Similar Threads

  1. Urgent! Java Assignment with loops in Arrays
    By Xeher in forum New To Java
    Replies: 4
    Last Post: 11-28-2010, 12:49 AM
  2. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM
  3. Printing Two Dimensional Arrays with for loops
    By mcnam4119 in forum JCreator
    Replies: 3
    Last Post: 10-06-2010, 05:27 AM
  4. A few questions about arrays and loops
    By Jamison5213 in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 05:59 AM
  5. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 12:24 PM

Posting Permissions

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