Created
April 26, 2012 13:10
-
-
Save fukayatsu/2499446 to your computer and use it in GitHub Desktop.
チャレンジプログラム4(JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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