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

2021/11/12

What is wrong with my code

johnsec johnsec

2021/11/12

#
public void act() { if (num3==Sum){ setImage(image1); } }
Gbasire Gbasire

2021/11/12

#
what do you want to do and what doesn't work ? Share full code if needed
johnsec johnsec

2021/11/12

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Square2 here. * * @author (your name) * @version (a version number or a date) */ public class Square2 extends Actor { private GreenfootImage image1; private GreenfootImage image2; public String num1 = Greenfoot.ask("What is number 1?"); public String num2 = Greenfoot.ask("What is number 2?"); public String num3 = Greenfoot.ask("What is number 1 plus number 2 ?"); public String Sum = num1 + num2; public void Square() { image1= new GreenfootImage("green.png"); image2= new GreenfootImage("yellow.png"); } /** * Act - do whatever the Square2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (num3==Sum){ setImage(image1); } } } It supposed to turn green when if they are qual to each other but its not
johnsec johnsec

2021/11/12

#
equal*
danpost danpost

2021/11/13

#
if ("3".equals(num1) && "4".equals(num2)) Sum = "34";
or
int num1 = "3";
int num2 = "4";
int Sum = num1+num2; // = "34"
It will not be 7.
You need to login to post a reply.