有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用另一个类从Java中的2D数组打印用户输入

我试图制作一个简单的棋盘,向用户询问棋盘上的坐标,例如a5或h7。当用户回答这个问题时,系统会提示用户输入一个棋子(目前可以是任何字符串,因为我不想检查它是否是有效的棋子),然后将其存储在该坐标中。理想情况下,在未来,当用户输入“完成”时,我希望这样做,然后程序将停止,下一个类将显示用户输入的数组的所有值,例如:

"Enter a coordinate: a5"
"Enter a piece: Lemon"
"Enter a coordinate: h8"
"Enter a piece: Queen"
"Enter a coordinate: Done"
>User is then prompted with, for example:
"You have entered Lemon at a5"
etc

这将是理想的输出,但任何帮助都将不胜感激。我现在的代码是:

import java.util.Arrays;
import java.util.Scanner;
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++);
            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 = row + col + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        String getGrid() {
            return this.grid;
        System.out.println(Arrays.deepToString(grid));
        }
    }

}
public class printArray
{
    System.out.println(getGrid() + "");
}

所以我试着返回数组,然后在循环结束时打印它,但我得到的编译器错误是:应在getGrid之后,标识符应位于“System.out.println(getGrid()+)”中线


共 (0) 个答案