服务器类和客户端类 - 多维数组

时间:2015-11-19 23:17:59

标签: java arrays class

我在这里的几个方面遇到了一些麻烦。首先,当阵列打印到屏幕时,“P”位于[0] [0] - 这是正常的,但对于任何其他单元格,它被'null'包围。我希望它充满破折号“ - ”。我还需要在无限循环中进行编码,要求用户输入“向上”,“向下”,“向左”,“向右”或“退出”。这个无限循环是否进入“Driver”类或“World”类,并且switch语句是否适用于此? 第二 - 不对行和列进行求和和显示。 “世界”课程是:

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 row: ");
        int crow = input.nextInt();
        System.out.println("Enter number of columns: ");
        int ccol = input.nextInt();
        array = new String[crow][ccol]; 
        array[0][0]=P;
    }

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

    }

}

“Driver”类是:

import java.util.Scanner;
public class Driver
{ 
    public static void main(String[] args)
    {        
        World world=new World();
        world.moveUp();
        world.moveDown();
        world.moveLeft();
        world.moveRight();
        world.displayWorld();

    }
} 

1 个答案:

答案 0 :(得分:1)

您需要初始化数组中的每个元素。将其添加到World构造函数的末尾:

   for(int i=0;i<crow;i++){
        for(int j=0;j<ccol;j++){
            array[crow][ccol]="-";
        }
    }

您需要在Driver类中添加要求输入的无限循环。在无限循环中,您需要:

  1. 获取输入
  2. 使用switch语句检测左,右等
  3. Driver.java:

    import java.util.Scanner;
    
    public class Driver {
    
        public static void main(String[] args) {
            World world = new World();
            Scanner s = new Scanner(System.in);
            while (true) {
                String input = s.nextLine();
                switch (input) {
                case "up":
                    world.moveUp();
                case "down":
                    world.moveDown();
                case "left":
                    world.moveLeft();
                case "right":
                    world.moveRight();
                }
            }
            s.close();
        }
    }