简体   繁体   English

二维 4x4 阵列中的邻居

[英]Neighbors in a 2D 4x4 Array

Alright... So I've been stuck with an exercise for school for a little while now and I really can't figure out how to solve it.好吧...所以我已经被学校练习困住了一段时间,我真的不知道如何解决它。 I think I've come really compared to where I started and I hope you guys could help me out.我认为与我开始的地方相比,我真的来了,我希望你们能帮助我。

The final meaning of the exercise would be that the code will output all the neighbors possible of every single digit in the array.练习的最终含义是代码将输出数组中每个数字的所有可能的邻居。 I've done the middle four ones, they work perfectly.我已经完成了中间的四个,它们完美地工作。 The outer digits are a pain for me, I can't find a way to make sure to make the code 'notice' that there are no more digits, for example, above the digit in the upper left corner.外部数字对我来说很痛苦,我找不到一种方法来确保代码“注意”没有更多数字,例如,在左上角的数字上方。

I feel like I know how to do it: with an if statement that doesn't let something happen if the value of the index of the array is higher than 3 or lower than 0. Because it's a 4x4 2D Array that means there are 0, 1, 2, 3 indexes for the X Axis and Y Axis.我觉得我知道该怎么做:如果数组的索引值高于 3 或低于 0,则使用 if 语句不会发生任何事情。因为它是一个 4x4 2D 数组,这意味着有 0 X 轴和 Y 轴的 , 1, 2, 3 索引。

I hope someone here is willing to help me out.我希望这里有人愿意帮助我。 It will be greatly appreciated!将不胜感激! Here's my code for so far!到目前为止,这是我的代码!

Thanks in advance提前致谢

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 {

                }

            }
        }
}      

} }

Here's a little hint:这里有一个小提示:

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]);
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM