在2d数组上获取null的输出不确定我在哪里出错

时间:2016-04-21 21:33:56

标签: java

我的编程课有问题而且我遇到了一个问题,我不确定在哪里修复。

问题在于:

World类将包含三个私有数据字段。第一个私有数据字段是2 代表世界的维度字符数组。世界的大小将取决于什么 传递给构造函数。下一个私有数据字段是characterRow和最后一个私有字段 数据字段是characterColumn。 构造函数将接受两个值,width和height。使用宽度和高度,设置大小 2维数组。然后用短划线填充数组, - 。 characterRow和 characterColumn数据字段都设置为0,然后将字符“P”放在位置 characterRow和characterColumn。 因此,如果您的世界要打印宽度为9或9列,高度为4或4行,那么它就会 像创作后的这样的东西:

P--------
---------
---------
---------

世界级最初将包含5种公共方法。这些方法是moveUp, moveDown,moveLeft,moveRight和displayWorld。

我得到了一个输出:

Enter number of rows: 
9
Enter number of columns: 
9

P - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 

 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  -  - 
Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program.
Enter command: 

我可以到达用户输入数组大小的点,然后数组打印出null,字符位于正确的位置,然后再次正确打印,没有字符。我不确定我在哪里偏离了轨道。你能看看并以任何方式帮助我吗?我的两个类的代码都在

之下

驱动程序类:

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {
    World world = new World();
    System.out.println("Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program.");
    System.out.println("Enter command: ");
    Scanner input = new Scanner(System.in);
    while (true) 
    {
        String s = input.nextLine();

            switch (s) 
            {
                case "up":
                    world.moveUp();
                    world.displayWorld();
                    break;
                case "down":
                    world.moveDown();
                    world.displayWorld();
                    break;
                case "left":
                    world.moveLeft();
                    world.displayWorld();
                    break;
                case "right":
                    world.moveRight();
                    world.displayWorld();
                    break;
                case "exit":
                    break;
            }
        }
    }
}

世界级:

import java.util.*;

public class World
{
private static final String  P = "P";
private String[][] array;
public World()
{        
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of rows: ");
    int crow = input.nextInt();
    System.out.println("Enter number of columns: ");
    int ccol = input.nextInt();
    array = new String[crow][ccol];
    array[0][0]=P;
    displayWorld();   
        for(int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < array[i].length; j++)
            {
                array[i][j] = new String();                    
            }
        }
    displayWorld();   
}

public void displayWorld()
{
    System.out.println();
        for(int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < array[i].length; j++)
            {
                System.out.print(array[i][j] + " - ");
            }
            System.out.println();
        }
}

public void moveUp()
{
   for(int i= 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if ((array[i][j]) == " - ")
            {
                if (i < array.length - 1)
                {
                    array[i][j] = " - ";
                    array[i - 1][j] = P;
                }                    
                return;
            }
        }
    }
}

public void moveDown()
{
    for(int i= 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if ((array[i][j]) == " - ")
            {
                if (i < array.length - 1)
                {
                    array[i][j] = " - ";
                    array[i + 1][j] = P;
                }                    
                return;
            }
        }
    }
}

public void moveLeft()
{
    for(int i= 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if ((array[i][j]) == " - ")
            {
                if (i < array.length - 1)
                {
                    array[i][j] = " - ";
                    array[i][j - 1] = P;
                }                    
                return;
            }
        }
    }

}

public void moveRight()
{
    for(int i= 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if ((array[i][j]) == " - ")
            {
                if (i < array.length - 1)
                {
                    array[i][j] = " - ";
                    array[i][j + 1] = P;
                }                    
                return;
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

    public World(){        
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of rows: ");
    int crow = input.nextInt();
    System.out.println("Enter number of columns: ");
    int ccol = input.nextInt();
    array = new String[crow][ccol];
    //array[0][0]=P;
    //displayWorld();   
        for(int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < array[i].length; j++)
            {
                if(i == 0 && j == 0){
                 array[i][j] = P;
                }else{
                 array[i][j] = new String();
                }                    
            }
        }
    displayWorld();   
    }

在构造函数中,在填充数组之前,您有一个displayWorld方法调用。然后,当你填充它时,你重新填充你的位置[0] [0]并且P消失。通过使用上面的代码,您将得到您想要的东西。

我希望我能帮到你!

相关问题