Jussper
Jussper

Reputation: 1

How can I get a method as a return value?

My homework is that my animal "Crab" should turn randomly. My randomly choosen number "n" should be the trigger for a turn. I am a beginner in programming sorry.

I tried to switch between boolean, int and void. It never worked.

import greenfoot.*; 

public class Crab extends Animal
{     
    public int zeahler; 
    public int n;* 

    public void act()
    {
       zeahler += zeahler; //zeahler wird hochgezählt
    }

    public boolean aendern()
    {
        n = Greenfoot.getRandomNumber(51);
        if(zeahler> n)  
        return turn(40);
    }

}

I expect the crab to turn by 40 degrees but I get the error: incompatible types: void cannot be converted to boolean

Upvotes: 0

Views: 72

Answers (1)

Eowalim
Eowalim

Reputation: 11

I think your turn(int) method should return void. However in your aendern() method you tell her to return void while it returns a boolean. Hence this error:"void cannot be converted to boolean"

You can try this :


public boolean aendern()
    {   
        boolean test = false;
        n = Greenfoot.getRandomNumber(51);
        if(zeahler> n) { 
         turn(40);
         test = true;
        }
       return test ;
    }

Upvotes: 1

Related Questions