2017-02-11 6 views
-2

いいえ...だから、私は少し時間のために学校の練習に立ち往生していましたが、本当に解決する方法が分かりません。私は私が始めた場所と比べて本当に来たと思います。皆さんが私を助けてくれることを願っています。二次元4x4アレイの隣人

練習の最終的な意味は、コードが配列内のすべての1桁の可能なすべての近隣を出力することです。私は中間の4つをやった、彼らは完全に動作します。外桁は私の痛みです。たとえば、左上隅の桁の上に桁がないことを「通知」するコードを作成する方法を見つけることができません。

私はそれを行う方法を知っているように私は感じて:それは4x4の2次元配列なので、配列のインデックスの値が3よりも高いまたは0よりも低い場合には、何かが起こることはできませんif文でX軸とY軸に0,1,2,3のインデックスがあることを意味します。

ここに誰かが私を助けてくれることを願っています。大歓迎です!これまでのところ私のコードです! 1> -1 && I - `場合((I:ここでは、事前に

おかげ

public class P620 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    int[][] counts = 
     { 
      { 1, 0, 3, 4}, 
      { 3, 5, 6, 4 }, 
      { 9, 7, 1, 4}, 
      { 1, 1, 1, 1} 
     }; 

    for(int i = 0; i <= 3; i++) { 

     for(int j = 0; j <= 3; j++) { 

      System.out.println("Neighbours van de array: " + i + j + " met waarde: " + counts[i][j]); 

       if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 

        System.out.println(counts[i - 1][j]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i - 1][j - 1]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i - 1][j + 1]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i][j - 1]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i + 1][j - 1]);   
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i + 1][j]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i + 1][j + 1]); 
       } 

       else if ((i < 4 && i > -1) && (j < 4 && j > -1)) { 
        System.out.println(counts[i][j + 1]); 
       } 
       else { 

       } 

      } 
     } 
}  

}

+0

私はあなたがこのような何かをしたいと思います+ 1 <4)&&(j - 1> -1 && j + 1 <4)) –

+0

あなたの質問は不明です。あなたは直接隣人または隣人2つの正方形を望んでいますか? – user3437460

+0

各桁の隣にあります。 – Wout

答えて

0

が少しヒントです:

for (int i=0; i < 4; ++i) { 
    for (int j=0; j<4; ++j) { 
     System.out.println("Neighbours van de array: " + i + j + " met waarde: " + counts[i][j]); 

     // print upper-left (northwest) neighbor, if there is one 
     if (i >= 1 && j >= 1) 
      System.out.println(counts[i-1][j-1]); 
     // print upper (north) neighbor, if there is one 
     if (i >= 1) 
      System.out.println(counts[i-1][j]); 
       . 
       . 
       . 
     // print lower (south) neighbor, if there is one 
     if (i < 3) 
      System.out.println(counts[i+1][j]); 
     // print lower-right (southeast) neighbor, if there is one 
     if (i < 3 && j < 3) 
      System.out.println(counts[i+1][j+1]); 
    }  
} 
+0

ありがとう、私はすぐにそれを調べるつもりです。 – Wout