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中命令行執行四則運算的功能
代碼如下:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.