Java中命令行执行四则运算的功能
2016-12-14 17:06
302 查看
代码如下:
package math; public class Algorithm { /** * @param args */ public static void main(String[] args) { String s=args[0]; if(s.indexOf("+")>-1){ String[] str=s.split("[+]"); if(str[0].matches("[0-9]+")&&str[1].matches("[0-9]+")){ System.out.println(Double.parseDouble(str[0])+Double.parseDouble(str[1])); } else{ System.out.println("操作数非数字"); } }else if(s.indexOf("-")>-1){ String[] str=s.split("[-]"); if(str[0].matches("[0-9]+")&&str[1].matches("[0-9]+")){ System.out.println(Double.parseDouble(str[0])-Double.parseDouble(str[1])); } else{ System.out.println("操作数非数字"); } }else if(s.indexOf("*")>-1){ String[] str=s.split("[*]"); if(str[0].matches("[0-9]+")&&str[1].matches("[0-9]+")){ System.out.println(Double.parseDouble(str[0])*Double.parseDouble(str[1])); } else{ System.out.println("操作数非数字"); } }else if(s.indexOf("/")>-1){ String[] str=s.split("[/]"); if(str[0].matches("[0-9]+")&&str[1].matches("[0-9]+")){ System.out.println(Double.parseDouble(str[0])/Double.parseDouble(str[1])); } else{ System.out.println("操作数非数字"); } }else { System.out.println("运算符非四则运算符(+,-,*,/)"); } } }以上代码可能存在一些冗余的部分,没有进行修改,如果需要可以自行拷贝和修改。
相关文章推荐
- java 四则运算,并可完成 自定义函数 表达式 功能
- Java 执行四则运算
- 结对编程——paperOne基于java的四则运算 功能改进
- eclipse编译的java程序命令行执行时提示找不到mysql驱动文件时的解决办法
- [转载]经典java转码程序,实现native2ascii命令行的功能
- Java代码实现四则运算
- 运用ANT地SQL Task来完成自己地Java执行SQL脚本文件地功能
- java 四则运算(没有文章的博客都有点击率,太对不起观众了囧)
- XPDF/pdftohtml/java执行命令行命令
- 打成jar包 在命令行下执行java工程
- 命令行执行java程序
- dos命令行执行java出错
- java实现一个整数和一个小数的四则运算和求最大值,平均值。
- Java的package语法功能导致的执行错误
- java中BigDecimal类的精确四则运算工具类Arith
- 带括号的四则混合运算的算符优先算法java实现
- java执行命令行的方法
- JAVA中的运算功能
- 栈如何运用在四则运算中(JAVA实现)
- java命令行编译与执行细节备忘