使用另一个类从java中的2d数组打印出用户输入

qvtsj1bj  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(225)

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

"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()+”“);”线

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题