我试图让编译器将数组从其中一个类传递给main方法。我不知道为什么它不起作用,代码看起来像这样:
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;
}
}
我已经尝试过以下几行:
new[] board = board.Createboard();
int board[] = board.Createboard();
{
int board = new board();
board = createBoard();
}
我知道我可以轻松地将所有内容放在一个班级甚至一个方法中,但我练习使用类,因此我创建了很多。
答案 0 :(得分:1)
int[] board2;
int userInput;
playBoard = board.createBoard();
userInput = takeAGuess.input();
playboard 定义在哪里?
而且......这么多课程!使用 Main 类中的方法,它可以让您的工作更轻松。