Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组值未按预期工作_Java_Arrays_Eclipse_Math - Fatal编程技术网

Java 数组值未按预期工作

Java 数组值未按预期工作,java,arrays,eclipse,math,Java,Arrays,Eclipse,Math,我正在使用地形表示制作一个栅栏,其中1是栅栏,0是空白。代码如下: package assignment_2; public class Fencing { public static void main(String[] args) { boolean b = true; int i; int j; int[] [] map = { {0, 1, 1, 0}, {1, 2,

我正在使用地形表示制作一个栅栏,其中1是栅栏,0是空白。代码如下:

package assignment_2;

public class Fencing {

public static void main(String[] args) {

    boolean b = true;
    int i;
    int j;
    int[] [] map = 
        {
                {0, 1, 1, 0},
                {1, 2, 1, 1},
                {1, 1, 1, 4},
                {1, 3, 0, 0}
        };
    for (i = 0; i < 4; i++){
        for (j = 0; j < 4; j++){
            if (map[i][j] != 1 && map[i][j] != 0){
                b = false;

                if (b == false){
                    System.out.println("Map does not have the correct format");
                    while (i < 4 && j < 4){
                        System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
                        i++;
                        j++;

                        }
                    }
                }
                else{
                System.out.println("The map is valid");
                return;

                }   
            }
        }       
    }
}
现在,这段代码在数组值之间滚动,如果存在不是1或0的值,那么它会将它们报告给用户。如果有错误,它会显示所有不正确的数字。它的问题是,在它拾取第一个错误后,它会沿对角线滚动数组的其余部分,并显示这些值,而不是显示不是1或0的错误数字。我认为这与代码的这一部分有关:

while (i < 4 && j < 4){
    System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
    i++;
    j++;

有人知道要更改什么吗?

您可以将程序修改为:

    for (i = 0; i < 4; i++){
        for (j = 0; j < 4; j++){
            if (map[i][j] != 1 && map[i][j] != 0){
               b = false; // greedy approach can break or return here as well
                        System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);

                        }
                    }
               if (b == false){
                    System.out.println("Map does not have the correct format"); }
                else{
                System.out.println("The map is valid"); }

作为输出,您希望最终实现什么?您是否在调试器中一次一行地遍历代码?它正在做你让它做的事情。您对输出的期望值是什么?是要收集所有值供以后使用并同时显示它们,还是必须就地进行扫描?从视觉上看,这没什么区别;从编码的角度来看,这有很大的区别。我希望它对每个错误都显示类似的内容:->在1,1处发现值2。它显示的是类似的东西,但不是实际的错误,而是在数组中沿对角线方向移动,并显示1,1 2,2 3,3值,当我希望它从左到右向下移动时。您确定的块确实是您的罪魁祸首。为什么会在那里?去掉while循环,去掉其中的incrementors。只保留println。为了更好地回答这个问题,您可以解释为什么原始索引不起作用,i++和j++意味着在每个错误上,第一个是在2,2,我们检查下一个索引2+1和2+1,或者在第一个匹配的情况下检查3,3
 for (i = 0; i < 4; i++) {
            //set b to true for each Map
            b = true;
            for (j = 0; j < 4; j++) {
                if (map[i][j] != 1 && map[i][j] != 0) {
                        //found incorrect value, set b to false
                        b = false;
                        System.out.println("Map is Invalid :"+map[i][j]);
                    }  
                }
                //If b is true, then ONLY Map is VALID
                if(b) {
                    System.out.println("Map is VALID");
                }
            }