我正在创建一个程序,其中存在随机生成的数字和随机生成的操作数,具体取决于用户选择的难度。我已经完成了数字和操作数的随机部分,但无法弄清楚如何计算出答案。任何帮助,将不胜感激。 谢谢。
public void generateNumbers(int diff) {
switch (diff) {
// Novice difficulty
case 0:
Random r = new Random();
Random x = new Random();
Random RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
Log.d("Brain Trainer", "operand = " + oper);
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?");
break;
// Easy difficulty
case 1:
Random ran = new Random();
int p = ran.nextInt(2);
Log.d("Brain Trainer", "Random num = " + p);
switch (p) {
case 0:
r = new Random();
x = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?");
break;
case 1:
r = new Random();
x = new Random();
Random z = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " =" + " ?");
break;
}
break;
//Medium difficulty
case 2:
ran = new Random();
p = ran.nextInt(3);
Log.d("Brain Trainer", "Random num = " + p);
switch (p) {
case 0:
r = new Random();
x = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?");
break;
case 1:
r = new Random();
x = new Random();
Random z = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " =" + " ?");
break;
case 2:
r = new Random();
x = new Random();
z = new Random();
Random c = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
oper2 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " =" + " ?");
break;
}
break;
// Guru difficulty
case 3:
ran = new Random();
p = ran.nextInt(3);
Log.d("Brain Trainer", "Random num = " + p);
switch (p) {
case 0:
r = new Random();
x = new Random();
Random z = new Random();
Random c = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
oper2 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " =" + " ?");
break;
case 1:
r = new Random();
x = new Random();
z = new Random();
c = new Random();
Random v = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
oper2 = operands[RandomOperand.nextInt(operands.length)];
oper3 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + oper3 + " "
+ v.nextInt(200) + " =" + " ?");
break;
case 2:
r = new Random();
x = new Random();
z = new Random();
c = new Random();
v = new Random();
Random h = new Random();
RandomOperand = new Random();
oper = operands[RandomOperand.nextInt(operands.length)];
oper1 = operands[RandomOperand.nextInt(operands.length)];
oper2 = operands[RandomOperand.nextInt(operands.length)];
oper3 = operands[RandomOperand.nextInt(operands.length)];
oper4 = operands[RandomOperand.nextInt(operands.length)];
equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " " +
" " + oper3 + " " + v.nextInt(200) + " " + oper4 + v.nextInt(200) + " =" + " ?");
break;
}
break;
}
}
答案 0 :(得分:0)
无论您生成的等式是什么,您可能需要以下列方式将该表达式的结果存储为特定类的数据成员:
使用JDK1.6 +,您可以使用内置的Javascript引擎。如,
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class App{
public static void main(String args[]) throws Exception{
ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName("JavaScript");
System.out.println(engine.eval("2+2"));
}
}