Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将S更改为一组命令行 请帮助我如何将字符串更改为一组命令行。请考虑下面的代码: public class TestStringCommand{ public static void main(String[] args) { System.out.println("Change String to Command"); int a , b, c; String S = "a =1 ; b =2; c = a + b;"; //How to change S into a set of command lines // c will be 3 } }_Java_String_Command - Fatal编程技术网

Java 如何将S更改为一组命令行 请帮助我如何将字符串更改为一组命令行。请考虑下面的代码: public class TestStringCommand{ public static void main(String[] args) { System.out.println("Change String to Command"); int a , b, c; String S = "a =1 ; b =2; c = a + b;"; //How to change S into a set of command lines // c will be 3 } }

Java 如何将S更改为一组命令行 请帮助我如何将字符串更改为一组命令行。请考虑下面的代码: public class TestStringCommand{ public static void main(String[] args) { System.out.println("Change String to Command"); int a , b, c; String S = "a =1 ; b =2; c = a + b;"; //How to change S into a set of command lines // c will be 3 } },java,string,command,Java,String,Command,一般来说,如果只是那些变量和加法,你可以去掉所有的空白,打开字符 int a, b, c; Pattern integer = Pattern.compile("-?\\d+"); //fields 主要内容: processStatement: String[] parts = stmnt.split("="); if ( parts.length != 2) throw new IllegalArgumentException(...); int val = evaluateExpress

一般来说,如果只是那些变量和加法,你可以去掉所有的空白,打开字符

int a, b, c; Pattern integer = Pattern.compile("-?\\d+"); //fields
主要内容:

processStatement:

String[] parts = stmnt.split("=");
if ( parts.length != 2) throw new IllegalArgumentException(...);
int val = evaluateExpression(parts[1].substr(0, parts[1].length()-1)); //remove the ';' 
setVar(parts[0], val);
setVar:

switch ( var ) {
   case "a": a = val; break;
   case "b": b = val; break;
   ...
   default:
      throw new IllegalArgumentException(...);
}
getVar:

switch ( var ) {
    case "a": return a;
    case "b": return b;
    ...
    default:
      throw new IllegalArgumentException (...)
}
evaluateExpression:

if (expr.isEmpty()) throw new ...;
int sum = 0; Matcher m = integer.matcher("");
for (String part :  expr.split("+")) {
    if (m.reset(part).matches()) {
        sum += Integer.parseInt(part);
    } else {
        sum += getVar(part);
    }
}
return sum;
我甚至没有尝试编译上面的代码,但这就是我的想法。你也可以使用虽然强大,但通常不是正确的方法

或者这就是你想要的

或者这就是你真正需要的

if (expr.isEmpty()) throw new ...;
int sum = 0; Matcher m = integer.matcher("");
for (String part :  expr.split("+")) {
    if (m.reset(part).matches()) {
        sum += Integer.parseInt(part);
    } else {
        sum += getVar(part);
    }
}
return sum;