Java 字符串转化成公式计算 (运算符:加+、减-、乘*、除/、求余%)

本文介绍了一种利用 Java 中的 ScriptEngine 接口进行数学运算的方法,包括加、减、乘、除及求余等基本操作,并通过实例展示了如何在 Java 程序中直接计算表达式的值。

今天在牛客网遇到一个题目,图片如下


计算:加+、减-、乘*、除/、求余%

快速寻找到方法:(阶乘自己写吧)

ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
		System.out.println("1+2的和为:"+js.eval("1+2"));
		System.out.println("1-2的差为:"+js.eval("1-2"));
		System.out.println("1*2的积为:"+js.eval("1*2"));
		System.out.println("1/2的商为:"+js.eval("1/2"));
		System.out.println("1%2的余为:"+js.eval("1%2"));

结果数据如下图:



{ /* CF_RULE */ "conditionType": 2 , "comparisonOperation": 0 , "formattingOptions": 0 , "formattingNotUsed": 0 , "fontFormatting": null , "borderFormatting": null , "patternFormatting": null , "formula1": { /* Formula */ "tokens": [ { /* RefNPtg */ "row": 0 , "rowRelative": true , "column": 0 , "colRelative": true , "formatReference": "RowOffset: 0 ColOffset: 0" }, { /* AreaPtg - index: 1 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* FuncPtg - index: 2 */ "functionIndex": 3 , "functionName": "ISERROR" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ }, { /* AttrPtg - index: 3 */ "volatile": false , "options": 2 /* OPTI_IF */ , "space_count": 0 , "space_type": 11 /* */ }, { /* AttrPtg - index: 4 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* StringPtg - index: 5 */ "value": "" }, { /* AttrPtg - index: 6 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 63 /* */ }, { /* AreaPtg - index: 7 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* FuncPtg - index: 8 */ "functionIndex": 129 /* 0x0081 */ , "functionName": "ISBLANK" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ }, { /* AttrPtg - index: 9 */ "volatile": false , "options": 2 /* OPTI_IF */ , "space_count": 0 , "space_type": 11 /* */ }, { /* AttrPtg - index: 10 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* StringPtg - index: 11 */ "value": "" }, { /* AttrPtg - index: 12 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 24 /* */ }, { /* AttrPtg - index: 13 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AreaPtg - index: 14 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* AttrPtg - index: 15 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AttrPtg - index: 16 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 3 /* CR_BEFORE_OPEN_PAREN */ }, { /* FuncVarPtg - index: 17 */ "functionIndex": 1 , "functionName": "IF" , "numberOfOperands": 3 , "externalFunction": false , "defaultOperandClass": 0 , "cetab": false }, { /* AttrPtg - index: 18 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AttrPtg - index: 19 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 3 /* CR_BEFORE_OPEN_PAREN */ }, { /* FuncVarPtg - index: 20 */ "functionIndex": 1 , "functionName": "IF" , "numberOfOperands": 3 , "externalFunction": false , "defaultOperandClass": 0 , "cetab": false }, { /* FuncVarPtg - index: 21 */ "functionIndex": 5 , "functionName": "AVERAGE" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ , "cetab": false }, { /* GreaterEqualPtg - index: 22 */ } ] , "expReference": null } , "formula2": { /* Formula */ "tokens": [ ] , "expReference": null } } 这段是什么意思
07-08
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值