繁体   English   中英

使用 Java 中的另一个 class 从二维数组打印用户输入?

[英]Print user inputs from 2D array using another class in Java?

我正在尝试在 Java 的另一个 class 中打印用户输入。 我制作了一个棋盘,要求用户在棋盘上输入字符串,然后,当这些字符串打印在屏幕上时,我希望 output 为“您已将棋子 [name] 放置在坐标 [coordinate]”。 我正在尝试在另一个 class 中而不是在主要方法中执行此操作,但是到目前为止我尝试的方法似乎不起作用。 这是我的代码。

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));
     }
}

所以我想做的是有一个新的 class 使用最后一个打印行来打印我之前提到的所需的 output。 任何帮助将不胜感激,谢谢!

编辑:

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));
            }
        }
    }
}

我有 2 个单独的文件 userInputs 和 showInput 但他们说它们仍应在单独的文件中声明?

It's wrong to write main Function in every class, the program uses the main function to start from it, So you should write it only in the main project class and call inside it the other classes. 您的代码应该是:

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;
    }
}

和另一个 class 到 output:

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
            }
        }
    }
}

就像@Atef Magdy 所说,您应该拥有一个包含所有数据和功能的 class 和一个执行功能的主 class。

以及对这个错误的解释(它指出使用 public int 是表达式的“非法开始”,并且它需要一个“;”在它之后?)我看到你制作了字符串类型的二维数组?

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

然后将其作为 int 类型的一维数组返回?

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

我应该说这是这个错误的根源。 您应该将 'int[]' 更改为 'string[][]'

如果有任何错误,请回复此答案!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM