简体   繁体   中英

How can I pass an Array from one class to another?

I am trying to make the compiler pass the array from one of the classes to the main method. I don't know why it does not work, the code looks like this:

public class Main {
    public static void main(String[] args) {

        int[] board2;
        int userInput;
        playBoard = board.createBoard();
        userInput = takeAGuess.input();



    }

}





import java.util.Scanner;
public class takeAGuess {
        int input()
        {
            int input=0;
            Scanner reader = new Scanner(System.in);
            System.out.println("Please enter your guess now");
            input = reader.nextInt();
            System.out.println("Guess entered successfully");
            return input;
        }
}







public class board {
    int[] createBoard()
    {
        int[] board = new int[7];
        int randomNum =(int) (Math.random()*5);
        for (int i=0; i<2; i++)
        {
            board[randomNum+i] = 1;
        }
        System.out.println("Board created");
        return board;
    }
}

I already tried these lines:

new[] board = board.Createboard();
int board[] = board.Createboard();
{
  int board = new board();
  board  = createBoard();
}

I am aware of that I could easily put everything in one class and even one method but i'm to practice on using classes therefore I create lots of them.

int[] board2;
int userInput;
playBoard = board.createBoard();
userInput = takeAGuess.input();

where is playboard defined?

And... so much classes! Use methods in the Main class instead, it'll make your job lighter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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