Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试制作一个棋盘,它接受简单的字符串输入并将它们存储在Java中_Java_Arrays - Fatal编程技术网

尝试制作一个棋盘,它接受简单的字符串输入并将它们存储在Java中

尝试制作一个棋盘,它接受简单的字符串输入并将它们存储在Java中,java,arrays,Java,Arrays,我还是Java新手,最近一直在使用2D数组。我想做的是制作一个简单的二维棋盘网格阵列,它的数组从a1一直到h8。我尝试做的第一件事是转换数组[8][8],使前8个是字符,后8个是整数。我目前试图做的是提示用户并询问他们想要选择哪个坐标(从a1一直到h8)。当他们选择一个坐标时,我希望他们输入一个字符串(将来将是我的棋子),该字符串将存储在棋盘上,这样,当我使用另一个类打印这些字符串时,它将给我一个输出,如“a5-Lemon(因为我还没有检查棋子是否有效)或“h2-Queen”。 因此,为了澄清,

我还是Java新手,最近一直在使用2D数组。我想做的是制作一个简单的二维棋盘网格阵列,它的数组从a1一直到h8。我尝试做的第一件事是转换数组[8][8],使前8个是字符,后8个是整数。我目前试图做的是提示用户并询问他们想要选择哪个坐标(从a1一直到h8)。当他们选择一个坐标时,我希望他们输入一个字符串(将来将是我的棋子),该字符串将存储在棋盘上,这样,当我使用另一个类打印这些字符串时,它将给我一个输出,如“a5-Lemon(因为我还没有检查棋子是否有效)或“h2-Queen”。 因此,为了澄清,该程序要求用户“请输入一个坐标(例如a5)”。“现在,请输入一个要放置在该坐标上的工件”,然后将其存储在该坐标中。当用户键入,例如“完成”时,我希望显示所有有工件的坐标以及工件上放置的工件

代码:

import java.util.Scanner;
公共类棋盘
{
公共静态void main(字符串[]args)
{
字符行='a';
弦斑;
扫描仪=新的扫描仪(System.in);
int[][]网格=新int[8][8];
对于(int i=0;i
我使用了while循环不断地提示用户,直到他们给出正确的坐标,但我不知道如何提示用户输入存储在该坐标中的工件


任何帮助都将不胜感激!

首先,为了用坐标存储工件名称,您需要一个二维字符串数组而不是整数

更改后,再次获取用户输入,然后将坐标与工件一起保存,并将其保存在所述列和行的2d数组中

此外,行是从字符(a-b-c-etc…)中检索的,列是从数字中检索的,而不是从路径中检索的

public class ChessBoard
{
     public static void main(String[] args)
    {
        char rows = 'a';
        String spot;
        Scanner scanner = new Scanner(System.in);
        String[][] grid = new String [8][8];

        for(int i = 0; i < grid.length; i++, rows++)
        {
            for(int col = 0; col < grid[i].length; col++)
            {
                System.out.print(rows + "" + (col + 1) + " ");

            }

            String input = null;              // will be changed to a valid position
            boolean validCoordinate = false;   // will be true if position is valid
            while ( ! validCoordinate) {
                System.out.println("Enter a coordinate (for example, a5): ");
                input = scanner.next();
                validCoordinate = input.matches("[a-g][1-8]");
            };
            // now we now that the input is valid
            int row = input.charAt(0) - 'a';
            int col = input.charAt(1) - '1';
            String temp = input + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        System.out.println(Arrays.deepToString(grid));
     }
}
公共类棋盘
{
公共静态void main(字符串[]args)
{
字符行='a';
弦斑;
扫描仪=新的扫描仪(System.in);
字符串[][]网格=新字符串[8][8];
对于(int i=0;i
非常感谢!我在System.out.println的最末端遇到了一个编译器错误,因为数组没有声明,我想?我应该用什么替换它?
导入java.util.Arrays;
在你的类之上你是一个传奇,非常感谢!啊,我遇到了另一个问题,我认为这与我最初的代码有关。每次都会遇到询问用户输入,它进入下一列,因此首先它会询问用户a1-8中的坐标,然后是b1-8,等等,直到它达到h1-8,然后它会继续询问用户一个工件,而不是他们想要放置工件的坐标。有什么想法吗?编辑了答案,所以现在我将坐标保存到另一个她的数组,然后迭代它,以便用户输入每个坐标的工件名称。
public class ChessBoard
{
     public static void main(String[] args)
    {
        char rows = 'a';
        String spot;
        Scanner scanner = new Scanner(System.in);
        String[][] grid = new String [8][8];

        for(int i = 0; i < grid.length; i++, rows++)
        {
            for(int col = 0; col < grid[i].length; col++)
            {
                System.out.print(rows + "" + (col + 1) + " ");

            }

            String input = null;              // will be changed to a valid position
            boolean validCoordinate = false;   // will be true if position is valid
            while ( ! validCoordinate) {
                System.out.println("Enter a coordinate (for example, a5): ");
                input = scanner.next();
                validCoordinate = input.matches("[a-g][1-8]");
            };
            // now we now that the input is valid
            int row = input.charAt(0) - 'a';
            int col = input.charAt(1) - '1';
            String temp = input + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        System.out.println(Arrays.deepToString(grid));
     }
}