Java 调用方法和返回值?抽搐
我的displayWinner方法无法正常工作。我相信我的调用没有按照我认为应该的方式工作,因为我不太熟悉使用多种方法,因为这个概念是最近才引入的 代码如下:Java 调用方法和返回值?抽搐,java,multidimensional-array,tic-tac-toe,Java,Multidimensional Array,Tic Tac Toe,我的displayWinner方法无法正常工作。我相信我的调用没有按照我认为应该的方式工作,因为我不太熟悉使用多种方法,因为这个概念是最近才引入的 代码如下: import java.util.*; public class tic { public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean flag=false;
import java.util.*;
public class tic
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean flag=false;
char[][] board =
{
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
boolean done = false;
int player = 1;
int row = 0;
int col = 0;
while (flag != true)
{
checkForWinner(board);
System.out.println("Enter the row and column for your next move");
row = in.nextInt();
col = in.nextInt();
if (player == 1)
{
board[row][col] = 'X';
player = 2;
checkForWinner(board);
}
else
{
board[row][col] = 'O';
player = 1;
checkForWinner(board);
}
printBoard(board);
checkForWinner(board);
}
displayWinner(player, flag);
}
public static void printBoard(char[][] board)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
System.out.print("|" + board[row][col] + "|");
}
System.out.println();
System.out.println("-------");
}
}
public static boolean checkForWinner(char[][] board)
{
// checkForWinner() method determines if a pattern of data stored
// in the 2 D char array indicates the a player has won the game.
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
boolean flag4 = false;
// checks the contents of each row for matching data
for (int i = 0; i <= 2; i++)
{
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) && board[i][2] != ' ')
flag1 = true;
}
// checks the contents of each column for matching data
for (int j = 0; j <= 2; j++)
{
if ((board[0][j] == board[1][j] && board[1][j] == board[2][j]) && board[2][j] != ' ')
flag2 = true;
}
// checks the contents of one diagonal for matching data
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) && board[2][2] != ' ')
flag3 = true;
// checks the contents of the other diagonal for matching data
if ((board[0][2] == board[1][1] && board[1][1] == board[2][0]) && board[2][0] != ' ')
flag4 = true;
// checks if any of the previous conditions evaluated to true
if (flag1 == true || flag2 == true || flag3 == true || flag4 == true)
flag = true;
// returns true if a winner was found; returns false is no winner
return flag;
} // end of checkForWinner method
public static void displayWinner(int player, boolean flag)
{
if (flag == true)
{
int currentplayer;
currentplayer=player;
System.out.println("The winner of the game is" +currentplayer);
}
}
}
import java.util.*;
公共类议会
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
布尔标志=假;
字符[][]板=
{
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
布尔完成=假;
int player=1;
int行=0;
int col=0;
while(flag!=true)
{
支票赢家(董事会);
System.out.println(“为下一步输入行和列”);
row=in.nextInt();
col=in.nextInt();
如果(玩家==1)
{
板[行][col]=“X”;
玩家=2;
支票赢家(董事会);
}
其他的
{
板[行][col]=“O”;
玩家=1;
支票赢家(董事会);
}
印刷板;
支票赢家(董事会);
}
显示赢家(玩家、旗帜);
}
公共静态无效打印板(字符[][]板)
{
对于(int行=0;行<3;行++)
{
for(int col=0;col<3;col++)
{
系统输出打印(“|”+板[行][列]+“|”);
}
System.out.println();
System.out.println(“----”);
}
}
公共静态布尔checkForWinner(字符[][]板)
{
//方法确定是否存储了数据模式
//在2D中,字符数组表示玩家已赢得游戏。
布尔标志=假;
布尔flag1=假;
布尔标志2=假;
布尔flag3=假;
布尔标志4=假;
//检查每行的内容是否有匹配的数据
对于(int i=0;i您没有更新main
方法中声明的标志
checkForWinner(board);
到
没有这个标志
将永远不会改变,你也永远不会离开你的while(flag!=true)
(可以简化为while(!flag)
)循环,这意味着你永远不会执行displayWinner(player,flag)
方法。你需要让标志真正改变你拥有的checkWinner(board)
当它应该是flag=checkWinner(board);
也可以调用函数两次(一次在循环开始时,一次在循环结束时),这是不需要的,并且应该在开始时取出一个。还有你的checkWinner(board)
方法不起作用,因为如果行或列具有相同的字符,并且在开始时它们都具有相同的字符(“”),则该方法将返回true因此,它将立即注册一个胜利,而不是让它检查一行或一列是否与一个玩家字符匹配。你的问题是什么?你认为它们应该以什么方式工作以及为什么?你还没有确定错误的来源,这是在修复错误之前需要完成的第一步。使用调试器来帮助你做到这一点他的。在更改了它并弄乱了if语句之后,我成功地修复了它。感谢您的回答,这实际上帮助我理解了调用不同方法的工作原理。
flag = checkForWinner(board);