简体   繁体   English

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

[英]Printing integer from 2D array using nested FOR Loops

I'm having a bit of a problem with a piece of Java code, part of an AI project. 我在AI项目的一部分Java代码中遇到了一些问题。 The programm is supposed to take a 11x13 2d array representing a maze. 该程序应该采用代表迷宫的11x13 2d数组。 The printed maze we are given uses characters for each cell, but for ease of use I've converted it to integers using a mnemonic code. 我们给的印刷迷宫为每个单元格使用字符,但是为了易于使用,我使用助记码将其转换为整数。

My problem is when I try to print the 2d integer array to the screen, to check eveything is OK, I get zeros at every cell, even though I have a check function in place which parses the array cell-by-cell checking for incorrect values. 我的问题是,当我尝试将2d整数数组打印到屏幕上以检查传送是否正常时,即使我有一个检查函数也能逐个检查该数组,以检查错误,即使每个单元格都为零,价值观。

The project is currently composed of 2 files. 该项目目前由2个文件组成。 The main file - function (AISemesterProject.java) and a file that will implement the UCS algorithm in the future (UCS.java) 主文件-函数(AISemesterProject.java)和将来将实现UCS算法的文件(UCS.java)

AISemesterProject.java AISemesterProject.java

package aisemesterproject;

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

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

UCS.java 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 输出量

输出量

As you can see the output is not what I expected and I have tried 4 different methods for the print (1 active, 3 commented) but the result is always the same. 如您所见,输出不是我所期望的,并且我尝试了4种不同的打印方法(有效1种,注释3条),但是结果始终相同。

Anyone have an idea what am I missing or doing wrong? 有人知道我在想什么或做错了什么吗?

Thanks for your time. 谢谢你的时间。

It looks like you have a scope issue... 看来您有范围问题...

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

you already created the array as a class level variable 您已经将数组创建为类级变量

Your constructor is setting the local variable array_int . 您的构造函数正在设置局部变量 array_int This local variable overshadows the field with the same name, and thus it never sees the array you're assigning to it. 此局部变量使具有相同名称的字段蒙上阴影,因此它永远不会看到您为其分配的数组。

You should make sure that you're assigning to the field, which can most easily be done by removing the int[][] word from your constructor. 您应该确保要分配给该字段,最简单的方法是从构造函数中删除int[][]字。

Thank you everyone. 谢谢大家。 I moved the declatarion from the constructor to the variable at the beggining and it worked. 在开始时,我将declatarion从构造函数移到了变量,并且它起作用了。

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

在此处输入图片说明

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

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