Hello there. I am trying to create a loop, which will move an objects position to a certain spot, based on a random number generator. Here is the code I have so far;

public void left()
   {

      if (this.getColour().equals(OUColour.WHITE)){
          while(this.getPosition() !== this.setPosition(this.getPosition() - getRandomGenerator().nextInt(5) + 1)){
              super.left();
            }
        }

}

So as you can see, I am trying to move the object a number of paces to the left, based on the the number generated from the random number generator. The movement must be done by using the getPos and setPos due to other methods restricting movement.

The errors I am receiving are 'illegal start of expression'. I also receive this same error when reduce the condition to simply this.getPos() !== this.setPos, making me think I have made a stupid mistake somewhere.

Any help is much appreciated. Thanks.

Recommended Answers

All 6 Replies

try with:

!=


also, your second number looks quite ... odd to me, don't really have time to figure out the logic, but it just does.

Ok Thanks for that. While that has fixed the initial issue, I am now left with a 'void type is not allowed here' when attempting to compile the code.

The idea behind the code, is that I have a frog with 11 black blocks he can stand on. When he goes past the far left block, he should instantly be moved to the far right box, and vice versa. This is done in the set.Position method.

This method should then move the frog (if he is white) a number of spaces to the left depending on the random number generator value. I understand however I may be going about this completely the wrong way, and would be more than happy for someone to enlighten me :)

yes, but I don't understand what that setter is doing there, and that is, if I'm not mistaken your 'void' issue.

you now have
if ( aNumber == ... )
a setter doesn't return anything, so there's nothing to compare it with

Ahhh ok I see, my apologies.

I have now changes this line of code to show;

while(this.getPosition() != this.getPosition() - getRandomGenerator().nextInt(5) + 1)

However now, the frog seems to be moving irregularly. The idea being that if for example the frog is on block 2, and the frog was instructed to move 4 spaces to the left, it would move two spaces to the left to block 0, then be moved to the far right hand block, and then be moved another 2 blocks to the left again. However, on certain tests the Frog seems to move upto 19 spaces in a single direction. I presume that this is due to the fact that the line will sometimes return negative numbers?

Can you see any other way of setting up this loop without causing these issues?

Sorry for all the questions - and thanks again for your help.

one issue I foresee is that you'll be stuck in an endless loop.
this.getPosition() will (as far as I can see) NEVER have the same value as
this.getPosition() - a random number (which is not zero)

for a lot more explanation, I think it's best if you show us more of your code, because this bit won't tell us much about what the rest of your code is doing, or, should be doing but isn't doing.

Thanks for the input, I have now however managed to solve the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.