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

2015/10/3

incompatible types int cannot be converted to boolean

Elexios Elexios

2015/10/3

#
i'm really new to java and greenfoot but how would i make this randomizer work int AppleSpawn = Greenfoot.getRandomNumber(10) ; if (AppleSpawn = 1) ## ill put code to do stuff here but that doesnt matter## i just want a randomizer that is between 1 and 10 but if it lands on 11 it does something but it says "incompatible types int cannot be converted to boolean". by the way i know what a boolean is and kind of know what the error means but cant seem to fix it. help?
danpost danpost

2015/10/3

#
The condition for an 'if' statement requires a boolean value. Your 'if' statement is assigning '1' as the value of the AppleSpawn variable and there is no boolean expression present. In other words you are not comparing the two, but actually setting the value of the variable. For comparison, you need to use the double equal sign, '==', a comparison operator, instead of the single one, '=', an arithmetic operator. Please refer to the java tutorial page on operators.
davmac davmac

2015/10/3

#
Please use code tags when posting code. The '=' operator is for assignment, so "if (AppleSpawn = 1)" would always set AppleSpawn to 1; the error is because 1 is not a boolean. You want '==', to compare the current value with 1, which returns a boolean and so solves the error as well. Finally, please note that by convention variable (and method) names in Java should begin with a lower-case letter - you should use 'appleSpawn' not 'AppleSpawn'. For class names, use an upper case character.
Elexios Elexios

2015/10/3

#
omg you guys are the best!!!
You need to login to post a reply.