我正在尝试获取一个返回一个值或另一个值的方法,并使用该答案在另一个方法中实现if子句。在我的isWinner方法中,我有:
public int isWin(int player)
{
if(((board[0][0]+board[0][1]+board[0][2]==3)||
(board[1][0]+board[1][1]+board[1][2]==3)||
(board[2][0]+board[2][1]+board[2][2]==3)||
(board[0][0]+board[1][0]+board[2][0]==3)||
(board[0][1]+board[1][1]+board[2][1]==3)||
(board[0][2]+board[1][2]+board[2][2]==3)||
(board[0][0]+board[1][1]+board[2][2]==3)||
(board[2][0]+board[1][1]+board[0][2]==3)))
{
return 1;
}
else if(((board[0][0]+board[0][1]+board[0][2]==6)||
(board[1][0]+board[1][1]+board[1][2]==6)||
(board[2][0]+board[2][1]+board[2][2]==6)||
(board[0][0]+board[1][0]+board[2][0]==6)||
(board[0][1]+board[1][1]+board[2][1]==6)||
(board[0][2]+board[1][2]+board[2][2]==6)||
(board[0][0]+board[1][1]+board[2][2]==6)||
(board[2][0]+board[1][1]+board[0][2]==6)))
{
return 2;
}
}
在我尝试返回1或2的地方。在另一种方法displayWinner()中,我的代码检查isWinner返回1或2,并根据isWinner()返回的内容打印出不同的文本:>
public void displayWinner()
{
if(isWin()==1)
{
System.out.println("\n Player 1 wins...!!");
isFull=true;
}
else if(isWin()==2)
{
System.out.println("\n Player 2 wins...!!");
isFull=true;
}
else
{
if(isFull)
{
System.out.println("It is a tie.");
}
}
}
如何返回1或2?每次我打电话给他们,我都会得到:
$ javac TicTacToe.java
TicTacToe.java:113: error: method isWin in class TicTacToe cannot be
applied to given types;
if(isWin()==1)
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
TicTacToe.java:118: error: method isWin in class TicTacToe cannot be
applied to given types;
else if(isWin()==2)
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length 2 errors
答案 0 :(得分:0)
根据您的代码,isWin(int player)方法需要传递一个参数,但是在调用时您没有向该方法传递参数。这就是为什么您会出错。
所以不要像isWin()那样调用,而是这样调用:isWin(1)或isWin(2)