DingDongDang132 DingDongDang132 - 1 month ago 10
Java Question

why am I getting the 'the operator && is undefined for the argument type(s) char,char' error

if ((board[x][x] && board[x + 1][x + 1] && board[x + 2][x + 2]) == 'Y') {
playerWins = true;
}


Why can't I use && and || here?

Answer

You want this:

if (board[x][x] == 'Y' && board[x + 1][x + 1] == 'Y' && board[x + 2][x + 2] == 'Y') {
    playerWins = true;
}

&& can only be used to join boolean expressions together.

Your code assumed some sort of distribution rule, like (x && y) == z being equivalent to (x == z) && (y == z). In English, you can state things that way "If x and y are both z," but programming languages (and formal logic) don't have such a definition.