Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Created April 26, 2012 13:10
Show Gist options
  • Save fukayatsu/2499446 to your computer and use it in GitHub Desktop.
Save fukayatsu/2499446 to your computer and use it in GitHub Desktop.
チャレンジプログラム4(JavaScript)
package main.java;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calc {
public static void main(String[] args) throws ScriptException {
if (args.length == 1) {
// 余計な文字を取り除く
String arg0 = args[0].replaceAll("'", "").replace("\"", "");
// 組み込みJavaScript Engine の Rhinoを使う
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("JavaScript");
System.out.println(engine.eval(arg0));
} else {
System.out.println("usage 1: java -jar Calc.jar 1+2*3");
System.out.println("usage 2: java -jar Cals.jar \"1 + 2*/3.0\"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment