Results 1 to 4 of 4
  1. #1
    rajuvishnu52 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Unhappy the code doesn't.i dont know why..plz help me.thanks in advance.

    import java.util.HashSet;
    import java.util.Random;

    /**
    *
    * @author Acer customer
    */
    class rangen {

    public int[][] ran(int[][] h) {

    Random randNumGenerator = new Random();
    int[][] x= new int[3][3];
    int f=h.length;
    HashSet<Integer> e=new HashSet<Integer>();


    for (int i=0; i<3; i++)
    {
    for (int j = 0; j < 3; j++) {


    int a= (randNumGenerator.nextInt(9)+1);
    h[i][j]=a;
    e.add(h[i][j]);
    while(e.size()!=f)
    {
    x[i][j]=h[i][j];

    }
    }
    }

    //System.out.println(x);
    return x;


    }
    public static void main(String[] args) {
    // TODO code application logic here
    int[][] q=new int[3][3];
    int[][] x=new int[3][3];

    rangen dok=new rangen();
    x=dok.ran(q);
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {


    System.out.println(x);
    }}


    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: the code doesn't.i dont know why..plz help me.thanks in advance.

    It's not clear what your code should do, please explain.

    Anyway, the while-loop never ends.

  3. #3
    Join Date
    Feb 2012
    Location
    Delhi, India
    Posts
    5
    Rep Power
    0

    Default Re: the code doesn't.i dont know why..plz help me.thanks in advance.

    its not working because it is entering an infinite while loop due to the condition in statement while (e.size()!=f) becoz on very first go, e.size=1 and f=3. So control enter looop, stuck there foreverrrrrrrrrrrrrrrrrrr....
    Remove this while loop, you will get an array returned of 3*3 size with all random numbers..
    Unable to understand the purpose of this additional while loop in your code while (e.size()!=f) { .... } ?

  4. #4
    rajuvishnu52 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: the code doesn't.i dont know why..plz help me.thanks in advance.

    i am trying to make a sudoku.so i need to have random no.s with out duplicates.the other while loop is to get 9 digits without duplication

Similar Threads

  1. My code doesn't work! Pane.showOptionDialog()
    By 0070071 in forum AWT / Swing
    Replies: 5
    Last Post: 01-18-2012, 06:56 AM
  2. Replies: 1
    Last Post: 11-21-2011, 08:46 PM
  3. Replies: 7
    Last Post: 11-22-2010, 03:38 PM
  4. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 06:53 AM
  5. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 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
  •