简体   繁体   English

用于在 android studio 中求解方程的犀牛

[英]rhino for equation solving in android studio

Im trying to make an app for school project.我正在尝试为学校项目制作一个应用程序。 the propose of the app is to solve equuations, the program is working pretty good (it's using newtons method).该应用程序的建议是解决方程,该程序运行良好(它使用牛顿法)。 when i did some research about how to make a string in to a code, ive found something called rhino, so now im using it to solve the equation the only problem is when im trying to enter the Math.pow(a, b) into the string i want to execute, string that look like this give me back this error:当我做了一些关于如何在代码中输入字符串的研究时,我发现了一个叫做 rhino 的东西,所以现在我用它来解决方程唯一的问题是当我试图输入 Math.pow(a, b) 时我想执行的字符串,看起来像这样的字符串给了我这个错误:

String => "(float)2*Math.pow(NaN,2)-1"
----------------------------------------------
Error => "org.mozilla.javascript.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1"

now I really didnt get rhino so good,so i tried to find any tutorial about it, and found nothing, any way, if any one how to make this work, i would really apriciate some help.现在我真的没有让犀牛那么好,所以我试图找到任何关于它的教程,但一无所获,无论如何,如果有人如何使这项工作,我真的会提供一些帮助。 Thanks Ido barel.谢谢伊多巴雷尔。

The full code if anyone need it.如果有人需要它的完整代码。

private Object place_X_in_func(String raw_func, float num){
        String func = raw_func.replace(" ", "");
        String equation = "(float)";
        int i = 0;
        while(i < func.length()) {
            boolean isPow = false;
            char c = func.charAt(i);
            if(c == 'x' || c == 'X'){
                char v = func.charAt(func.indexOf(c)+1);
                if((int)v == 94){
                    char z = func.charAt(func.indexOf(v)+1);
                    equation += "Math.pow("+String.valueOf(num)+","+z+")";
                    i+= 3;
                    isPow = true;
                }
                else{
                    equation+= String.valueOf(num);
            }
            }
            else {
                equation += c;
            }
            if(!isPow){
                i += 1;
            }
            //System.out.println("Runned "+i+"of "+func.length());
        }
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
        try {
            System.out.println("EQ => "+equation);
            Object result = engine.eval(";"+equation);
            System.out.println(result);
            return ("R:"+result);
        } catch (ScriptException e) {
            System.out.println(e.getMessage());
        }
        return  0;
    }

Your string isn't valid javascript.您的字符串不是有效的 javascript。 Javascript has no datatype casting. Javascript 没有数据类型转换。 If you remove the (float) at the beginning of your string it should work.如果您删除字符串开头的(float) ,它应该可以工作。 The result of the expression 2*Math.pow(NaN,2)-1 in javascript is a number . javascript 中表达式2*Math.pow(NaN,2)-1的结果是一个number The result will be returned to java as a java.lang.Double .结果将作为java.lang.Double返回给 java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM