首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android随机生成方程答案

Android随机生成方程答案
EN

Stack Overflow用户
提问于 2017-03-04 20:47:54
回答 1查看 85关注 0票数 0

我正在创建一个程序,其中有随机生成的数字和随机生成的操作数,这取决于用户选择的难度。我已经做了数字和操作数的随机部分,但不知道如何计算出答案。任何帮助都将不胜感激。谢谢。

代码语言:javascript
复制
   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;

    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-04 21:26:50

无论您生成了什么等式,您都可能需要将该表达式的结果存储为特定类的数据成员,方法如下:

通过JDK1.6+,你可以使用内置的Javascript引擎。例如,

代码语言: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"));
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42596205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档