java 错误:二元运算符“&&”的错误操作数类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31120805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:08:39  来源:igfitidea点击:

error: bad operand types for binary operator '&&'

java

提问by JT attack

Not sure what is wrong here is the line that wrong.

不确定这里有什么问题是错误的行。

if((board[z][i] = 1) && (board[z][i++] = 1) && (board[z++][i] = 1)){

Here is my whole code not finished:

这是我未完成的整个代码:

public class Solution {

 static void nextMove(int player, int [][] board){

}

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int player;
    int board[][] = new int[8][8];

    //If player is 1, I'm the first player.
    //If player is 2, I'm the second player.
    player = in.nextInt();

    //Read the board now. The board is a 8x8 array filled with 1 or 0.
  for(int z = 0; z < 8; z++){
      for(int i = 0; i < 8; i++) {
          board[(z)][(i)] = in.nextInt();
         }
  }for(int z = 0; z < 8; z++){
       for(int i = 0; i < 8; i++) {
           if((board[z][i] = 1) && (board[z][i++] = 1) && (board[z++][i] = 1)){
               System.out.print(z + " " + i);
        }
    }
  }
    nextMove(player,board);

}
}

回答by java is life

You need to use the relational operator == instead of =. Each of the 3 sub-statements within the parentheses of your if statement should represent a boolean. In Java, = is used to assign a value and == is used to check equality. Therefore, you must change your if statement to:

您需要使用关系运算符 == 而不是 =。if 语句括号内的 3 个子语句中的每一个都应表示一个布尔值。在 Java 中,= 用于赋值,而 == 用于检查相等性。因此,您必须将 if 语句更改为:

if((board[z][i] == 1) && (board[z][i++] == 1) && (board[z++][i] == 1)){

Also, rather than incrementing z and i by 1 (because you are already doing that as part of your for loop), maybe make i++ --> i + 1 and z++ --> z + 1.

此外,与其将 z 和 i 递增 1(因为您已经将其作为 for 循环的一部分),还不如使 i++ --> i + 1 和 z++ --> z + 1。

Hope this helps!

希望这可以帮助!

回答by KDP

if((board[z][i] ==1) && (board[z][i++] == 1) && (board[z++][i] == 1)){

use double equals for comparing. I think you have missed it :-)

使用双等号进行比较。我想你已经错过了:-)

回答by Michael

You're using the assignment operator, when you should be comparing them.

当您应该比较它们时,您正在使用赋值运算符。

if((board[z][i] == 1) && (board[z][i++] == 1) && (board[z++][i] == 1)){

回答by Sneh

if((board[z][i] = 1) && (board[z][i++] = 1) && (board[z++][i] = 1)) //Yes this line is wrong

You are using '='at (board[z][i] = 1)(board[z][i++] = 1)(board[z++][i] = 1)at these comparisons. You need to use '=='operand for comparison.

您正在使用“=”,在(board[z][i] = 1)(board[z][i++] = 1)(board[z++][i] = 1)这些比较。您需要使用'=='操作数进行比较。

An '='operator is used for assigning the values to the variables whereas '=='is used for comparison. Just to add one more point, when comparing values of two Stringsyou have to use .equals()method as '=='compares by reference in Java on objects.

一个“=”运算符用于分配值给变量而“==”被用于比较。再补充一点,当比较两个字符串的值时,您必须使用.equals()方法,因为'=='在 Java 中按引用比较对象。

回答by Héctor E

Shouldn't your code be

你的代码不应该是

if((board[z][i] == 1) && (board[z][i++] == 1) && (board[z++][i] == 1)){

if((board[z][i] == 1) && (board[z][i++] == 1) && (board[z++][i] == 1)){

emphasizing the ==issue

强调==问题

回答by Sourav Kanta

You used the assignment (=) operator instead of checking if they were equal. The equality is checked with == operator. Try this instead

您使用了赋值 (=) 运算符而不是检查它们是否相等。使用 == 运算符检查相等性。试试这个

if((board[z][i] ==1) && (board[z][i++] == 1) && (board[z++][i] == 1)){