Java 为什么我的数组从一个方法返回为空,但代码在主方法中运行时工作正常?

Java 为什么我的数组从一个方法返回为空,但代码在主方法中运行时工作正常?,java,arrays,class,multidimensional-array,methods,Java,Arrays,Class,Multidimensional Array,Methods,我的程序有以下问题。 它是一个“连接四个”Java控制台应用程序。 启动程序时,我重置了3项内容 多维字符数组 public final int行=8,列=8 public char[][]board=新字符[行][列] 我用for循环重置它,用字符“.”覆盖每个数组字段,这是我的默认板纹理 public char[][] resetBoard() { // for loops cycle through every array field for (in

我的程序有以下问题。 它是一个“连接四个”Java控制台应用程序。 启动程序时,我重置了3项内容

  • 多维字符数组
  • public final int行=8,列=8

    public char[][]board=新字符[行][列]

    我用for循环重置它,用字符“.”覆盖每个数组字段,这是我的默认板纹理

        public char[][] resetBoard() {
            // for loops cycle through every array field
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    board[i][j] = '.';
                }
            }
            return board;
        }
    
  • 整数

    public int roundCounter=0

  • 整数记录当前游戏中有多少回合。我想在玩的时候把它打印出来

        public int resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            return roundCounter = 0;
        }
    
    我将这些重置为如下所示:

            gameMain main = new gameMain();
            gameMode mode = new gameMode();
            gameCheck check = new gameCheck();
    
            main.board = main.resetBoard();
            main.nums = main.resetNums();
            check.roundCounter = check.resetRoundCounter();
    
    public int resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            return 0;
    }
    
    resetRoundCounter(); //this function changes the value of your variable.
    
    roundCounter = resetRoundCounter(); 
    
    我的问题是在打印游戏板时,nums阵列和圆形计数器似乎都不起作用。 游戏板完全是空白的。nums数组仅为0,且圆形计数器保持为0。 当在main方法中运行代码时,它比在类中运行要好

    我的打印方法:

        public void printBoard() {
            gameMain main = new gameMain();
            gameCheck check = new gameCheck();
    
            // Printing number array with space in between the elements
            for (int i = 0; i < main.nums.length; i++) {
                System.out.print(main.nums[i] + " ");
            }
            // Printing the round count next to the number array
            System.out.println("            Round " + check.getRoundCounter());
    
            for (int i = 0; i < main.rows; i++) {
                for (int j = 0; j < main.columns; j++) {
                    System.out.print(main.board[i][j] + " ");
                }
                System.out.println();
            }
            for (int i = 0; i < main.nums.length; i++) {
                System.out.print(main.nums[i] + " ");
            }
            System.out.println("\n");
        }
    
    public void打印板(){
    gameMain main=新gameMain();
    gameCheck check=新gameCheck();
    //打印元素之间有空格的数字数组
    for(int i=0;i
    我真的需要一些帮助,因为我整晚都没睡。最初是因为我在编程时有多有趣,现在它变得令人沮丧


    感谢阅读并提前感谢

    我认为发生在你身上的事情与你正在使用的对象的引用有关。你混合了两种不同的工作方式,我举个例子:

    您可以使用“roundCounter”的引用并使用它:

    public void resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            roundCounter = 0;
    }
    
    或者您可以退回,如下所示:

            gameMain main = new gameMain();
            gameMode mode = new gameMode();
            gameCheck check = new gameCheck();
    
            main.board = main.resetBoard();
            main.nums = main.resetNums();
            check.roundCounter = check.resetRoundCounter();
    
    public int resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            return 0;
    }
    
    resetRoundCounter(); //this function changes the value of your variable.
    
    roundCounter = resetRoundCounter(); 
    
    在第一种情况下,您必须像这样调用函数:

            gameMain main = new gameMain();
            gameMode mode = new gameMode();
            gameCheck check = new gameCheck();
    
            main.board = main.resetBoard();
            main.nums = main.resetNums();
            check.roundCounter = check.resetRoundCounter();
    
    public int resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            return 0;
    }
    
    resetRoundCounter(); //this function changes the value of your variable.
    
    roundCounter = resetRoundCounter(); 
    
    在第二种情况下,您必须像这样调用函数:

            gameMain main = new gameMain();
            gameMode mode = new gameMode();
            gameCheck check = new gameCheck();
    
            main.board = main.resetBoard();
            main.nums = main.resetNums();
            check.roundCounter = check.resetRoundCounter();
    
    public int resetRoundCounter() {
            // Resetting Round Counter, by initializing it to 0
            return 0;
    }
    
    resetRoundCounter(); //this function changes the value of your variable.
    
    roundCounter = resetRoundCounter(); 
    
    您可以选择您喜欢的工作方式,但我建议您不要使用全局变量,尤其是使用方法。我希望这对你有帮助。

    照我说的做

    public void resetBoard() {
        // for loops cycle through every array field
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                board[i][j] = '.';
            }
        }
    }
    
    public void resetNums() {
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i + 1;
        }
    }
    
    public void resetRoundCounter() {
        // Resetting Round Counter, by initializing it to 0
        roundCounter = 0;
    }
    

    我还建议您遵循,例如,
    gameMain
    应该是
    gameMain
    gameCheck
    应该是
    gameCheck

    M0e-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。公认的答案有助于未来的访问者自信地使用解决方案。检查以了解如何做。