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

ddrv8njm  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(273)

我试图在java的另一个类中打印用户输入。我制作了一个棋盘,要求用户在棋盘上输入字符串,然后,当这些字符串被打印到屏幕上时,我希望输出是“您已将工件[名称]放置在坐标[坐标]处”。我试图在另一个类中,而不是在主方法中这样做,但我迄今为止尝试的似乎不起作用。这是我的密码。

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-h][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));
     }
}

所以我想做的是有一个新类,它使用最后一行来打印我前面提到的所需的输出。任何帮助都将不胜感激,谢谢!
编辑:

import java.util.Arrays;
import java.util.Scanner;
public class ChessBoard1
{
    public static  void main(String[] args)
    {
        userInputs input = new userInputs();
        showInput show = new showInput();

        String grid[][] = input.takeInput();
        show.show(grid);
    }
}

public class userInputs
{
    public String[][] takeInput()
    {
        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-h][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;
        }
        return  grid;
    }
}

public class showInput {

    public void show(String [][] inputs)
    {
        for(int i=0 ; i<inputs.length ; i++){
            for(int j=0  ; j < inputs[0].length ; j++)
            {
                System.out.println(Arrays.deepToString(grid));
            }
        }
    }
}

我有两个单独的文件userinputs和showinput,但他们说,他们仍然应该在一个单独的文件中声明?

63lcw9qa

63lcw9qa1#

就像@atef magdy所说的,应该有一个类来保存所有的数据和函数,还有一个主类来执行函数。
以及对这个错误的解释(它声明使用public int是表达式的“非法开始”,它需要一个“;”之后?)我看到你做了字符串类型的二维数组?

String[] [] grid = new String [8][8];

然后作为int类型的1d数组返回?

public int[] getGrid(){
return grid.clone();
}

我应该说这是这个错误的根源。您应该将“int[]”更改为“string[][]”
如果有任何错误,请回复此答案!

af7jpaap

af7jpaap2#

在每个类中编写main函数是错误的,程序使用main函数从它开始,所以您应该只在主项目类中编写它,并在它内部调用其他类。您的代码应该是:

package com.company;

public class ChessBoard
{
    public static  void main(String[] args)
    {
        userInputs input = new userInputs();
        showInput show = new showInput();

        String grid[][] = input.takeInput();
        show.show(grid);
    }
}

以及其他单独文件中的类,如:

package com.company;

import java.util.Scanner;

public class userInputs
{
    public String[][] takeInput()
    {
        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-h][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;
        }
        return  grid;
    }
}

以及另一个要输出的类:

package com.company;

public class showInput {

    public void show(String [][] inputs)
    {
        for(int i=0 ; i<inputs.length ; i++){
            for(int j=0  ; j < inputs[0].length ; j++)
            {
                //Print Your Data
            }
        }
    }
}

相关问题