开发者

Java Nonworking method?

Hey, So Ive got this method to determine if a game I wrote is over and for some reason it isn't working. I'm getting the error "the left hand side of assignment must be a variable"

Here is the method:

public boolean isWinner() {//checks for game completion
     int check = board[startX][startY];
     for(int i=0; i<sizeX; i++){
        for(int j=0; j<sizeY; j++){
           if (board[i][j] != check)
              return false;
        }
     }
     return true;
  }

Here is a piece of the code where it is used:

ColorFloodGame game = new ColorFloodGame (6,6);  开发者_开发技巧      
while (game.isWinner() = false){
    //code here
}


You have while (game.isWinner() = false), which is an assignment because you have a single = sign.

You want while (game.isWinner() == false) which is a comparison because it has two = signs.


You need to replace

while (game.isWinner() = false){

with

while (game.isWinner() == false){

or better yet:

while (!game.isWinner()){


while (game.isWinner() = false){

should be

while (game.isWinner() == false){

one '=' is an assignment, two '==' is a comparison


Ralf & Gabe have already answered it, but IMHO this is more readable:

while (!game.isWinner()){


The expression in the while statement is an assignment. The compiler can't evaluate the expression correctly. Change your code to the following and everything should work fine.

while (game.isWinner() == false) {
    //code here
}

You could also write the code like this

while (!game.isWinner()) {
    //code here
}

The style used is different for every programmer and you should find your own preference.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜