使用嵌套的FOR循环从2D数组打印整数

时间:2016-11-03 22:33:06

标签: java arrays maze

我对一段Java代码有点问题,这是AI项目的一部分。该程序应该采用代表迷宫的11x13 2d阵列。我们给出的印刷迷宫使用每个单元格的字符,但为了便于使用,我使用助记符代码将其转换为整数。

我的问题是当我尝试将2d整数数组打印到屏幕上时,检查eveything是否正常,我在每个单元格都得到零,即使我有一个检查功能,它逐个解析数组检查错误的值。

该项目目前由2个文件组成。主文件 - 函数(AISemesterProject.java)和将来实现UCS算法的文件(UCS.java)

AISemesterProject.java

package aisemesterproject;

public class AISemesterProject
{
    public static void main(String[] args)
    {
        UCS a = new UCS();

        a.checkArrayInt();
        a.printInt();
    }
}

UCS.java

package aisemesterproject;

import java.util.Arrays;

public class UCS
{
    int row = 11;
    int col = 13;
    int[][] array_int = new int[row][col];

    public UCS()
    {
        // Lets assume
        // x = 0
        // e = 1
        // d = 2
        // s = 8
        // g = 9
        int[][] array_int = new int[][] {
            {0,1,0,1,1,1,1,0,0,1,0,9,0},
            {1,1,1,2,0,1,1,0,0,1,2,1,0},
            {0,1,0,1,1,1,1,0,0,1,0,0,0},
            {8,1,2,0,1,2,0,1,1,2,1,1,1},
            {0,0,1,1,0,1,1,1,0,0,0,0,0},
            {1,2,1,0,1,0,1,1,0,0,1,1,1},
            {0,1,2,0,1,0,0,2,1,1,2,1,9},
            {1,0,1,1,2,1,1,1,0,1,1,1,1},
            {1,1,2,1,1,0,0,1,0,0,0,0,0},
            {0,0,1,1,1,0,0,1,1,1,1,1,2},
            {0,0,1,0,0,1,1,1,0,9,0,1,1}
        };
    }

    public void checkArrayInt()
    {
        int i = 0, j = 0;
        boolean checker = false;

        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                if(!(array_int[i][i] == 0 || array_int[i][j] == 1 || array_int[i][j] == 2 || array_int[i][j] == 8 || array_int[i][j] == 9))
                {
                    checker = true;
                    System.out.print("Error at Row:" + i + " Column:" + j + "\n");
                }
            }
        }

        if(checker == false)
        {
            System.out.print("Array OK... \n");
        }
    }

    public void printInt()
    {
        int i = 0, j = 0;

        //System.out.println(Arrays.deepToString(array_int));

        for(i = 0; i < row; i++)
        {
            System.out.print("Row " + (i + 1) + ":");

            for(j = 0; j < col; j++)
            {
                System.out.print(" " + String.valueOf(array_int[i][j]));
                //System.out.print(" " + Integer.toString(array_int[i][j]));
                //System.out.printf(" %d", array_int[i][j]);
                //System.out.print(" " + array_int[i][j]);
            }

            System.out.print("\n");
        }
    }
}

输出

Output

正如你所看到的那样输出不是我所期望的,我已经尝试了4种不同的打印方法(1个有效,3个评论),但结果总是一样。

任何人都知道我错过了什么或做错了什么?

感谢您的时间。

3 个答案:

答案 0 :(得分:0)

看起来你有一个范围问题......

int[][] array_int = new int[row][col];

public UCS()
{
    // Lets assume
    // x = 0
    // e = 1
    // d = 2
    // s = 8
    // g = 9
     array_int = new int[][] {
        {0,1,0,1,1,1,1,0,0,1,0,9,0},
        {1,1,1,2,0,1,1,0,0,1,2,1,0},
        {0,1,0,1,1,1,1,0,0,1,0,0,0},
        {8,1,2,0,1,2,0,1,1,2,1,1,1},
        {0,0,1,1,0,1,1,1,0,0,0,0,0},
        {1,2,1,0,1,0,1,1,0,0,1,1,1},
        {0,1,2,0,1,0,0,2,1,1,2,1,9},
        {1,0,1,1,2,1,1,1,0,1,1,1,1},
        {1,1,2,1,1,0,0,1,0,0,0,0,0},
        {0,0,1,1,1,0,0,1,1,1,1,1,2},
        {0,0,1,0,0,1,1,1,0,9,0,1,1}
    };

您已经将数组创建为类级别变量

答案 1 :(得分:0)

您的构造函数正在设置局部变量 array_int。这个局部变量使用相同的名称覆盖了该字段,因此它永远不会看到您分配给它的数组。

您应该确保分配到字段,这可以通过从构造函数中删除int[][]字来轻松完成。

答案 2 :(得分:0)

谢谢大家。我将构造函数中的declatarion移动到了beggining处的变量,并且工作正常。

package aisemesterproject;

import java.util.Arrays;

public class UCS
{
    int row = 11;
    int col = 13;

    // Lets assume
    // x = 0
    // e = 1
    // d = 2
    // s = 8
    // g = 9

    int[][] array_int = new int[][] {
        {0,1,0,1,1,1,1,0,0,1,0,9,0},
        {1,1,1,2,0,1,1,0,0,1,2,1,0},
        {0,1,0,1,1,1,1,0,0,1,0,0,0},
        {8,1,2,0,1,2,0,1,1,2,1,1,1},
        {0,0,1,1,0,1,1,1,0,0,0,0,0},
        {1,2,1,0,1,0,1,1,0,0,1,1,1},
        {0,1,2,0,1,0,0,2,1,1,2,1,9},
        {1,0,1,1,2,1,1,1,0,1,1,1,1},
        {1,1,2,1,1,0,0,1,0,0,0,0,0},
        {0,0,1,1,1,0,0,1,1,1,1,1,2},
        {0,0,1,0,0,1,1,1,0,9,0,1,1}
    };

    public UCS()
    {
        // Lets assume
        // x = 0
        // e = 1
        // d = 2
        // s = 8
        // g = 9

        // Array initialization outside the constructor scope
    }

    public void checkArrayInt()
    {
        int i = 0, j = 0;
        boolean checker = false;

        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                if(array_int[i][j] == 0) //Check for 0 = x
                {
                    checker = false;
                }
                else if(array_int[i][j] == 1) //Check for 1 = e
                {
                    checker = false;
                }
                else if(array_int[i][j] == 2) //Check for 2 = d
                {
                    checker = false;
                }
                else if(array_int[i][j] == 8) //Check for 8 = s
                {
                    checker = false;
                }
                else if(array_int[i][j] == 9) //Check for 9 = g
                {
                    checker = false;
                }
                else //All other integers, which are false
                {
                    checker = true;
                    System.out.print("Error at Row:" + i + " Column:" + j + "\n");
                }
           }
        }

        if(checker == false)
        {
            System.out.print("Array OK... \n");
        }
   }

    public void printInt()
    {
        int i = 0, j = 0;

        //System.out.println(Arrays.deepToString(array_int));

        for(i = 0; i < row; i++)
        {
             System.out.print("Row " + (i + 1) + ":");

            for(j = 0; j < col; j++)
            {
                System.out.print(" " + String.valueOf(array_int[i][j]));
                //System.out.print(" " + Integer.toString(array_int[i][j]));
                //System.out.printf(" %d", array_int[i][j]);
                //System.out.print(" " + array_int[i][j]);
            }

            System.out.print("\n");
        }
    }
}

enter image description here

相关问题