Java Android随机生成方程答案
我正在创建一个程序,其中有随机生成的数字和随机生成的操作数,这取决于用户选择的难度。我已经做了数字和操作数的随机部分,但不知道如何计算出答案。任何帮助都将不胜感激。 谢谢Java Android随机生成方程答案,java,android,math,Java,Android,Math,我正在创建一个程序,其中有随机生成的数字和随机生成的操作数,这取决于用户选择的难度。我已经做了数字和操作数的随机部分,但不知道如何计算出答案。任何帮助都将不胜感激。 谢谢 public void generateNumbers(int diff) { switch (diff) { // Novice difficulty case 0: Random r = new Random(); Rand
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;
}
}
无论生成了什么等式,您都可能需要以以下方式将该表达式的结果存储为特定类的数据成员: 使用JDK1.6+可以使用内置的Javascript引擎。e、 g
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"));
}
}
只需保存随机生成的数字并执行该操作。选择数字和运算符后,只需进行计算。每个操作员都需要一段不同的代码。