首页 > 解决方案 > (JAVA)我必须将一个字符放入数组中,然后我必须移动字符(扫描仪)

问题描述

我必须在数组中放置一个 X,然后我必须移动 X(左、上、右、下)。我想我必须用 java.util.Scanner 来做。我需要帮助。

我用数组创建了一个字段,点是我可以移动我的 X(播放器)的字段。

规则:我不能移动到 R 或 O

public class Main {

    public static void main(String[] args) {

        final int WIDTH = 18;
        final int HEIGHT = 11;
        final int CHAR_COUNT = 30;
        final int PLAYER = 1;
        final int endgame = 1;
        final int PLAYERCOUNTER = 2;
        final char[] CHARS = {
            'R',
            'O'
        };
        final char[] PLAYERPOINTS = {
            'X',
            '+'
        };
        String player = "X";

        // Create field filled with '.'
        char[][] field = new char[HEIGHT][WIDTH];

        for (int i = 0; i < HEIGHT; i++) {
            Arrays.fill(field[i], '.');


        }

        // the holes and robots (R and O)
        Random random = new Random();
        for (char ch: CHARS) {

            for (int j = 0, x, y; j < CHAR_COUNT; j++) {

                do {
                    x = random.nextInt(WIDTH);

                    y = random.nextInt(HEIGHT);
                } while (field[y][x] != '.');
                field[y][x] = ch;

            }
        }

        //print field
        for (char[] row: field) {
            System.out.println(Arrays.toString(row).replaceAll(",", "") + " ");

        }

        //Player
        Scanner input = new Scanner(System.in);
    }
}

标签: javaarraysloops

解决方案


推荐阅读