【发布时间】:2017-09-23 19:51:09
【问题描述】:
我可以使用用户输入的数字打印 2 个二维数组,也可以打印奇数,但是我无法将两种形式的代码组合在一起,以便奇数与它们的单元格保持一致而不仅仅是在一个打印行中。我将如何打印奇数,在 2 个 3x3 数组中将非赔率留为空白? 下面是打印数组的代码:
public static void display ( int[][] FirstArray, int[][] SecondArray)
{
//Print first array
System.out.print("Array1: \n");
for (int row = 0; row < FirstArray.length; row++)
{
for(int column = 0; column < FirstArray[row].length; column++)
{
System.out.print(FirstArray[row][column] + " ");
}
System.out.println();
}
//Print second array
System.out.print("Array2: \n");
for (int row = 0; row < SecondArray.length; row++)
{
for(int column = 0; column < SecondArray[row].length; column++)
{
System.out.print(SecondArray[row][column] + " ");
}
System.out.println();
}
}
ex output:
array 1: array2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
这里是打印奇数的代码,没有像上面的代码那样以 3x3 格式打印:
public static void display(int[][] FirstArray, int[][] SecondArray)
{
int count=0;
for (int i = 0; i < FirstArray.length; i++) {
for (int j = 0; j < FirstArray.length; j++)
{
if(FirstArray[i][j]%2==1)
{
System.out.println(m1[i][j]);
}
}
}
for (int i = 0; i < SecondArray.length; i++) {
for (int j = 0; j < SecondArray.length; j++)
{
if(SecondArray[i][j]%2==1)
{
System.out.println(SecondArray[i][j]);
}
}
}
ex 输出: 3 3 3 3 3 3 3 3 3(显示奇数但在一行中)
Ex output of what Im looking for(assuming i entered in even numbers too):
3 3 3
3 3 3
3 3
【问题讨论】:
标签: java for-loop multidimensional-array printing