- Home /
Having random operators in an equation?
Hey everyone, I'm making a math game that generates random equations, the twist is that plus is equal to minus, multiply is equal to divide and vise versa. While I can get the game to work with just two numbers, e.g. 2+2, I cannot seem to make the game generate a random equation with more than 2 numbers, e.g 2+2-3.
Does anyone have any idea how I can randomly generate the operators in the equation and have unity calculate the answer?
Script it. If you know how to program this is very easy
Answer by Owen-Reynolds · Jun 05, 2013 at 03:16 PM
There are languages where you can say things like w="-";
print mathResult(7 w 3);`, but it would take much longer to learn them than to do it the long way.
Think about this to get started:
int mathResult(int n1, string op, int n2) {
if(op=="+") return n1+n2;
if(op=="X") return n1*n2;
....
}
Then you can find the result of "a op1 b op2 c" using: r1=mathResult(a, op1, b); answer=mathResult(r1, op2 c);
Your answer
Follow this Question
Related Questions
How do I compare two binary values in Javascript? 1 Answer
Random.seed Repeating 2 Answers
Using arrays to randomly select an object 0 Answers
Script not working (Java script) 2 Answers
Code to randomly generate a mesh? 0 Answers