我正在制作一个 TicTacToe 游戏,其中包含初始化游戏、显示棋盘、显示游戏选项、说出轮到谁、检查获胜者、添加一步、重新启动游戏、检查棋盘是否已满的方法,并玩游戏。我在添加移动方法和玩游戏方法时遇到问题。
public boolean addMove(int row, int column) {
boolean nonacceptable = true;
while (nonacceptable) {
System.out.println("Which row and column would you like to enter your mark? Enter the row and column between 0 and 2 separated by a space.");
row = input.nextInt();
column = input.nextInt();
if ((row >= 0 && row <=2) && (column >= 0 && column <=2)) { //make sure user entered a number between 0 and 2
if (gameBoard[row][column] != ' ')
System.out.println("Sorry, this position is not open!");
else {
gameBoard[row][column] = currentMark;
nonacceptable = false;
}
}
else
System.out.println("That position is not between 0 and 2!");
}
return nonacceptable;
}
这是我的玩法:
public void letsPlay() {
while (true) {
displayBoard();
gameOptions();
int choice = input.nextInt();
if (choice == 1) {
if (addMove()) {
displayBoard();
checkWinner();
System.exit(0);
}
else if (!boardFull()) {
displayBoard();
System.out.println("Board full!");
System.exit(0);
}
else {
whoseTurn();
}
}
else if (choice == 2)
restart();
else if (choice == 3)
System.exit(0);
else
System.out.println("Try again!");
}
}
当我编译时,我收到此错误:TicTacToe.java:110: error: method addMove in class TicTacToe can not be apply to given types; 如果(添加移动()){ ^ 必需:整数,整数 发现:没有参数 原因:实际参数列表和正式参数列表的长度不同 1 个错误
我该如何解决这个问题?
最佳答案
很清楚了。
您的 addMove 函数签名接受两个参数
public boolean addMove(int row, int column) {
^ ^
每当您想要调用或使用 addMove 函数时,您都必须遵循您在签名函数中定义的规则。
因此,解决方案是在调用 addMove 函数的地方传递两个 int 类型的参数,这样问题就消失了。
注:Read more about how to define a function and call it in Java
关于Java-如何解决我的 TicTacToe 游戏的方法无法应用于给定类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32897367/