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

2021/9/30

Error: bad operand types for binary operator

xixEmilyxix xixEmilyxix

2021/9/30

#
I have code where im trying to make it so a certain variation of the simulation runs depending on the temperature and i am getting an error with the if statements. These are in one of the worlds - beeWorld.
    public void runSimulation()
    {
        if(weatherValue >= -40 || weatherValue <= 15)
        {
            //if weatherValue is between -40 and 15 then run the low temperature simulation            
            lowTemperature();
        } else if(weatherValue >= 16 || weatherValue <= 25)
        {
            //if weatherValue is between 16 and 25 then run the regular temperature simulation            
            regularTemperature();
        } else if(weatherValue >= 26 || weatherValue <= 40)
        {
            //if weatherValue is between 26 and 40 then run the high temperature simulation            
            highTemperature();
        } else {
            //if values arent set, bring back the main menu
            Greenfoot.setWorld(new mainMenu());
        }
    }
i have weatherValue defined in the code aswell as this:
    weatherInput weatherValue;
Does anyone know how to fix this error?
MrBradley MrBradley

2021/9/30

#
Can you provide the definition of weatherInput? Generally, you cannot use relational operators (>=, <, etc.) with your own types. They are only defined for numeric primitives (int, double, etc). Also, it is always useful to provide the error you are getting.
xixEmilyxix xixEmilyxix

2021/9/30

#
public class weatherInput extends Actor
{
    //create variable
    int weatherValue;
    /**
     * Act - do whatever the weatherInput wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      //Create input box, ask for value, set bounds for weatherValue
      setImage(new GreenfootImage("Input.png"));
      
      //show weatherValue in the box on main menu
      getWorld().showText("" + weatherValue, 400, 250);
      
      //show weatherValue in the box on the main simulation screen
      getWorld().showText("" + weatherValue, 400, 250);
      
      //set value of weather based off the input
        if(Greenfoot.mouseClicked(this))
        {
            weatherValue = Integer.parseInt(Greenfoot.ask("Please input a value for the weather"));
        if(weatherValue < -40 || weatherValue > 40)
        {
            World world = getWorld();
            world.addObject(new errorMessageweather(), 350, 200);
        } else {
            World world = getWorld();
            world.removeObjects(world.getObjects(errorMessageweather.class));
        } 
      }
    }
}
this is the code i have in my weatherinput class
xixEmilyxix xixEmilyxix

2021/9/30

#
the error is: bad operand types for binary operator '>=' first type: weatherInput second type: int
danpost danpost

2021/10/1

#
xixEmilyxix wrote...
the error is: bad operand types for binary operator '>=' first type: weatherInput second type: int
You have weatherValue defined as a weatherInput object in your beeWorld. The value of weatherValue held by your weatherInput object would then be:
int wv = weatherValue.weatherValue;
You should probably define the object in beeWorld with:
weatherInput wi;
and then use:
int wv = wi.weatherValue;
in the method of beeWorld.
danpost danpost

2021/10/1

#
By convention, class names should begin with an uppercase letter (BeeWorld, WeatherInput, etc.) and field names with a lowercase letter (weatherValue). Then you can declare the object in BeeWorld with:
WeatherInput weatherInput;
and use the following within the method:
int weatherValue = weatherInput.weatherValue;
(the weatherInput field should be assigned a WeatherInput object prior to using code in method -- assigned either in the constructor of your BeeWorld or in the declaration line) That way, when you see something beginning with an uppercase letter, it will probably be a class name and when you see something beginning with a lowercase letter, it will probably be a field name (you can keep then identifiable that way).
xixEmilyxix xixEmilyxix

2021/10/6

#
Thank you, and especially for the tip I changed those over :D
You need to login to post a reply.