题解 | #简单计算器#
简单计算器
http://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239
import java.util.Scanner; import java.lang.String; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str=sc.nextLine(); String[] strs=str.split("[+\\-*/]"); if(strs.length<2)//先判断再分 { System.out.println("Invalid operation!"); }else{ double a=Double.parseDouble(strs[0]); String F=str.substring(strs[0].length(),strs[0].length() + 1); double b=Double.parseDouble(strs[1]); switch(F) { case "+":System.out.printf("%.4f+%.4f=%.4f",a,b,(a+b));break; case "-":System.out.printf("%.4f-%.4f=%.4f",a,b,(a-b));break; case "*":System.out.printf("%.4f*%.4f=%.4f",a,b,(a*b));break; case "/": if(b==0) { System.out.println("Wrong!Division by zero!");break; }else{ System.out.printf("%.4f/%.4f=%.4f",a,b,(a/b));break; } } } } }