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

2018/2/22

How can you create random maths questions to pop up?

ChloeT ChloeT

2018/2/22

#
I am creating a game for Primary School children as part of my coursework. The game includes a character collecting objects, e.g. fruit. When a piece of fruit is collected, I want a random Maths question to pop up so they can answer it. If it is answered correctly, I want a point to be added to a score system. I am very new to Greenfoot, so have no clue how to do this. I have a character that is collecting fruit, so only need help with the random maths question and the score system. If anyone could help, I would really appreciate it.
Vercility Vercility

2018/2/22

#
Declare 3 new variables
int op1, op2, result;
Randomize their value
import java.util.Random; // this goes at the very top

Random rand = new Random(); // this goes where u determine that a fruit has been picked up

a = rand.nextInt(b) + a; //lower bound a upper bound b
b = rand.nextInt(b) + a;

result = a+b;
If you want different arithmetic operations use another variable to determine which one it is e.g 0 for addition, 1 subtraction etc Last thing you need is to pop up a window that will Display the values a/b and a field to enter you result I do not know exactly how to do this the best so I'll let Dan handle it from here
ChloeT ChloeT

2018/2/22

#
How do you create the pop up window?
xbLank xbLank

2018/2/22

#
Either you just use
Greenfoot.ask(String question) //returns string as result
or you create another world that will be shown whenever you want to ask the User a question.
Yehuda Yehuda

2018/2/23

#
Also, I see that people use a JOptionPane, but it would be easier to use Greenfoot.ask, and the JOptionPane cannot be converted into HTML 5.
You need to login to post a reply.