I am trying to create an nxn array full of random integers between 1 and 10. When I try to print it out, I am getting an odd number of integers not filling an array, and never up to the correct number of integers (for instance, a supposed 5x5 array is returning 17 integers). Code snippet follows, assume all variable are declared correctly unless contained in here and java.util.Random is imported.
if (choice==1){
Random rand = new Random();
System.out.println("Please input a power n for (nxn array) between 1-6");
int power = kb.nextInt();
int[][] randMatrix = new int[power-1][power-1];
if (power < 1 || power > 6){
System.out.println("Invalid power");
}else{
for (i=0; i<randMatrix.length; i++){
for (j=0; j<randMatrix.length; j++){
randMatrix[i][j] = rand.nextInt(9);
}
}for (i=0; i<randMatrix.length; i++){
for (j=0; j<randMatrix.length; j++){
System.out.println(randMatrix[i][j]);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire