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

2021/3/11

Math Problem randomizer won't work

ConorK ConorK

2021/3/11

#
public class MathProblem extends Enemy
{
    
    /**
     * Act - do whatever the MathProblem wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a = Greenfoot.getRandomNumber(10);
    int b = Greenfoot.getRandomNumber(10);
    int correct = a + b;
    int incorrect = Greenfoot.getRandomNumber(20);
    public MathProblem()
    {
       
       
       
    }
    public void act() 
    {
            
            randomProblem();
            
            
        
    } 
    
    
    
    public void randomProblem()
    {
        
        setImage(new GreenfootImage("What is " + a + "+" + b, 50, Color.WHITE, Color.BLACK));
    }
    
    public void randomCorrect()
    {
       
           setImage(new GreenfootImage(correct + "", 50, Color.WHITE, null));
       
    }
    
    public void randomIncorrect()
    {
              
        
        
        setImage(new GreenfootImage(incorrect + "", 50, Color.WHITE, null));
        
        
    }
Every time I run it, the problem shows up but the correct answer is different when it runs. Does it randomize again in a method or something else?
danpost danpost

2021/3/11

#
Lines 8 thru 13:
int a;
int b
int correct;
int incorrect;
At line 31:
a = Greenfoot.getRandomNumber(10);
b = Greenfoot.getRandomNumber(10);
correct = a+b;
incorrect = Greenfoot.getRandomNumber(18);
if (incorrect >= correct) incorrect++;
danpost danpost

2021/3/11

#
ConorK wrote...
Does it randomize again in a method or something else?
As you had it, the values were only set once upon creation of the MathProblem object.
danpost danpost

2021/3/11

#
I guess my lines 4 and 5 could be replaced with:
incorrect = (correct+1+Greenfoot.getRandomNumber(18))%19;
You need to login to post a reply.