简单的四则运算

代码如下:
import java.util.*;
public class Test {
        public static void main(String...args){
            ArrayList<String> al=new ArrayList<String>();
            System.out.println("请输入要计算的式子个数:");
            Scanner sc=new Scanner(System.in);
            int count=sc.nextInt();
            System.out.println("请输入你要求解的式子:");
            for(int i=0;i<count;i++){
                sc=new Scanner(System.in);
                al.add(sc.nextLine());
            }
            for(int i=0;i<count;i++){
                System.out.print(al.get(i)+"=");
                f(al.get(i));
            }
        }
        public static void f(String s){
            if(s.indexOf("+")>-1){
                String[] str=s.split("[+]");
                System.out.println(Double.parseDouble(str[0])+Double.parseDouble(str[1]));
            }else if(s.indexOf("-")>-1){
                String[] str=s.split("[-]");
                System.out.println(Double.parseDouble(str[0])-Double.parseDouble(str[1]));
            }else if(s.indexOf("*")>-1){
                String[] str=s.split("[*]");
                System.out.println(Double.parseDouble(str[0])*Double.parseDouble(str[1]));
            }else if(s.indexOf("/")>-1){
                String[] str=s.split("[/]");
                System.out.println(Double.parseDouble(str[0])/Double.parseDouble(str[1]));
            }else if(s.indexOf("%")>-1){
                String[] str=s.split("[%]");
                System.out.println(Integer.parseInt(str[0])%Integer.parseInt(str[1]));
            }
        }
}

 

运行结果为:

posted @ 2018-03-09 10:55  SNIHUO  阅读(424)  评论(0)    收藏  举报