【发布时间】:2015-11-16 01:47:38
【问题描述】:
我正在尝试打印一个带有随机数的 2d 网格和另一个相同大小的网格,其中 -1 代替可被 3 整除的数字。我完成了几乎所有内容,但第二个网格仅打印 -1 .我究竟做错了什么?如何让第二个网格仅打印数字可被 3 整除的 -1?谢谢!
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);
}
}
【问题讨论】: