我正在创建一个程序,其中有随机生成的数字和随机生成的操作数,这取决于用户选择的难度。我已经做了数字和操作数的随机部分,但不知道如何计算出答案。任何帮助都将不胜感激。谢谢。
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;
}
}发布于 2017-03-04 21:26:50
无论您生成了什么等式,您都可能需要将该表达式的结果存储为特定类的数据成员,方法如下:
通过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"));
}
}https://stackoverflow.com/questions/42596205
复制相似问题