我正在制作一款《TicTacToe》游戏,其方法包括初始化游戏、显示棋盘、显示游戏选项、显示轮到谁了、检查赢家、添加移动、重新启动游戏、检查棋盘是否满了以及玩游戏。我的add a move方法和play game方法有问题。
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;
}
这是我的play方法:
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:错误:方法addMove类TicTacToe不能应用于给定的类型;if (addMove()) {^要求:int, int发现:无参数原因:实际的和正式的论证列表长度不同1错误
我该如何解决这个问题?
这很清楚。
你的addMove函数签名接受两个参数
public boolean addMove(int row, int column) {
^ ^
当你想调用或使用addMove函数时,你必须遵循你在签名函数中定义的规则。
所以,解决方案是传递两个参数,类型为int在你调用addMove函数的地方,这个问题就解决了
注意:阅读更多关于如何在Java中定义和调用函数的信息
您只声明了接受两个int参数的addMove
方法。
public boolean addMove(int row, int column) { ...
如果你没有没有参数的addMove
声明(public boolean addMove() {
),你不能这样调用它:addMove()
.
根据您的代码,您不需要参数,因为您将从Scanner
中为它们赋值,因此将方法声明更改为:
public boolean addMove() {
//declare the variables
int row, column;
...
}