I'm trying to print a 2d grid with random numbers and another equally sized grid with -1s in place of the numbers that are evenly divisible by 3. I finished pretty much everything, but the second grid is only printing -1s. What am I doing wrong? How can I get the second grid to only print -1s where the numbers are evenly divisible by 3? Thanks!
public class practice
{
public int[][] createArray(int rSize, int cSize) {
Random r = new Random();
int[][] array = new int[rSize][cSize];
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[0].length; col++) {
array[row][col] = r.nextInt(25);
}
}
return array;
}
public void print2DArray(int[][] Array) {
for (int row = 0; row < Array.length; row++) {
for (int col = 0; col < Array[0].length; col++) {
System.out.print(Array[row][col] + "\t");
}
System.out.println("\n");
}
}
public int[][] createCoords(int[][] Array) {
int[][] coord = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
for (int row = 0; row < coord.length; row++) {
for (int col = 0; col < coord[0].length; col++) {
System.out.print(coord[row][col] + "\t");
}
System.out.println("\n");
}
for (int row = 0; row < coord.length; row++) {
for (int col = 0; col < Array[row].length; col++) {
if (Array[row][col] % 3 == 0) coord[row][col] = -1;
}
}
return coord;
}
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
practice c = new practice();
int[][] myArray;
myArray = c.createArray(10, 10);
c.print2DArray(myArray);
c.createCoords(myArray);
}
}
It looks like you're printing the coords
array before you're checking for Array
elements being a multiple of 3, plus you're initializing coords
to all -1 so changing an element of coords
to -1 does... nothing. Initialize coords
to all 0's (which is the default, so you don't need to initialize it at all, just declare it) then print it after checking Array
.
A few style things: First, initial uppercase identifiers are generally reserved for classes, in fact there's already a java.lang.reflect.Array
in the standard class library. Use array
instead, or better yet, use a name that describes it's use , not it's type. Second, you've already got a print2DArray
method, why not use it in createCoords
? Finally, coords
needs to be the same size as Array
(or whatever you rename it to :-) ), so you should declare it as such. If you change the size you pass to createArray
, you have to remember to change coords
as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.