2017-09-23 5 views
0

私は2次元配列をユーザー入力からの数字で印刷することができますし、奇数も印刷することができますが、奇数がセルと一貫性を保つように2つのコード形式を組み合わせる際に問題があります。 1つの印刷ラインだけではありません。 2x3x3配列の空白として非オッズを残す奇数をどのように出力しますか? 相続人アレイ印刷するためのコード:ユーザ入力から2次元配列javaに奇数をプリントするには?

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ここ

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の形式でなし奇数を印刷するためのコードであります3 3 3 3 3(奇数番号が表示されますが、1行で)

Ex output of what Im looking for(assuming i entered in even numbers too): 
3 3 3 
    3  3 3 
3   3 

答えて

0

はあなたにループの両方であなたのprint文を是正することができます

for (int i = 0; i < FirstArray.length; i++) { 
    for (int j = 0; j < FirstArray.length; j++) { 
    if(FirstArray[i][j]%2==1) { 
     System.out.print(m1[i][j] + " "); // odd number and space 
    } else { 
     System.out.print(" "); // blank space for even numbers 
    } 
    } 
    System.out.println(); // next line for next row 
} 
関連する問題