importjava.util.Scanner;
importjava.math.BigDecimal;
publicclassMain{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
while(scanner.hasNext()){
Stringstr=scanner.nextLine();
if(!str.contains("+")&&!str.contains("-")&&!str.contains("*")&&!str.contains("/"))
System.out.println("Invalidoperation!");
else{
String[]s=str.split("[+|\\-|*/]");
BigDecimalnum1=newBigDecimal(s[0]),num2=newBigDecimal(s[1]);
if(str.contains("+"))
System.out.println(String.format("%.4f%s%.4f=%.4f",num1,"+",num2,num1.add(num2)));
elseif(str.contains("-"))
System.out.println(String.format("%.4f%s%.4f=%.4f",num1,"-",num2,num1.subtract(num2)));
elseif(str.contains("*"))
System.out.println(String.format("%.4f%s%.4f=%.4f",num1,"*",num2,num1.multiply(num2)));
elseif(str.contains("/"))
if(num2.compareTo(BigDecimal.ZERO)==0)
System.out.println("Wrong!Divisionbyzero!");
else
System.out.println(String.format("%.4f%s%.4f=%.4f",num1,"/",num2,num1.divide(num2,4,BigDecimal.ROUND_HALF_UP)));
}
}
}
}
收藏
一键三连
扫一扫,分享海报