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);
}}
}
}
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.
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) { .... } ?
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