This site requires JavaScript, please enable it in your browser!
Greenfoot back
CH1
CH1 wrote ...

2019/12/5

Random Attack help

CH1 CH1

2019/12/5

#
I am trying to create a scenario where the boss chooses a random attack from 1-3 but I can't seem to figure out the code using getRandomNumber is leading me the right way but I cant get past that
danpost danpost

2019/12/5

#
CH1 wrote...
I am trying to create a scenario where the boss chooses a random attack from 1-3 but I can't seem to figure out the code using getRandomNumber is leading me the right way but I cant get past that
What did you try/
CH1 CH1

2019/12/6

#
I have tried to use getRandomNumber
danpost danpost

2019/12/6

#
CH1 wrote...
I have tried to use getRandomNumber
Plus one?
CH1 CH1

2019/12/6

#
no the actual string was public void randomAttack() { Greenfoot.getRandomNumber(2); if(getRandomNumber = 1) { } }
danpost danpost

2019/12/6

#
From what I can tell from what was given, getRandomNumber appears to be an undefined variable. Also, the expression for the if condition looks to be an int value, not a boolean one (which is required for a condition). Another problem is the previous line which is just a loose int value -- it is like coding a line like:
 //   0;
or
//   1;
(both uncommented) which is really equivalent to
;
a non-operative statement. Create a local int variable and assign the random value to it:
int randomNumber = Greenfoot.getRandomNumber(2);
Then you can compare it to 1 with:
if (randomNumber == 1)
Note the double equal sign used to compare for equality (not like your single one which actually would assign 1 to the variable and not perform any comparisons).
You need to login to post a reply.