簡體   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