Java中使用javascript实现计算器功能
不废话,直接上代码:Java代码
package org.galo.thread;
import java.util.Scanner;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calculator {
private static final String WELCOME_MESSAGE = "请输入计算表达式:(或'exit'退出)";
private static final String EXIT_MESSAGE = "系统正在退出,请稍后";
private static final String QUIT_MESSAGE = "感谢您的使用,再见!";
private static final String ERROR_MESSAGE = "[表达式语法错误]请输入:(或'exit'退出)";
public static void main(String[] args) {
sayWelcome();
main();
}
public static void main() {
Scanner scanner = new Scanner(System.in);
println(WELCOME_MESSAGE);
while(scanner.hasNext()){
String exp = scanner.next();
long startTime = System.currentTimeMillis();
if("exit".equals(exp)){
doExit();
return;
}
String result = evalJS(exp);
if(result == null){
continue;
编辑推荐:
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>