自己编写的计算器

import java.util.Scanner;
import org.junit.Test;

/*
 * 实现一个简单的计算器功能,并且将数字转化为大写
 * @Param a 运算数字a
 * @Param calculating 运算符号+,-,*,/
 * @Param b 运算数字b
 * @Return 获得计算值
 * "零","壹","貮","叁","肆","伍","陆","柒","捌","玖"
 * 提示:int a,int b>=0,如出现异常情况返回:'-1'不需要转换
 * 		如:a=99,calculating="+";b=1(a+b=100) 结果为100,返回结果为壹零零
 */
public class Calculator {
	
	public String calc(int a,int b,String calculating){
		int sum = 0;
		try {
			if(calculating.equals("+")){
				sum = a + b;
			}else if(calculating.equals("-")){
				sum = a - b;
			}else if(calculating.equals("*")){
				sum = a * b;
			}else if(calculating.equals("/")){
				sum = a / b;
			}
		} catch (Exception e) {
			
			return "-1";
		}
		return String.valueOf(sum);
		
	}
	public String getCalcValue(String str){
			String temp = "";
			for (int i = 0; i < str.length(); i++) {
				switch (str.charAt(i)) {
				case '1':
					temp+="壹";
					break;
				case '2':
					temp+="貮";
					break;
				case '3':
					temp+="叁";
					break;
				case '4':
					temp+="肆";
					break;
				case '5':
					temp+="伍";
					break;
				case '6':
					temp+="陆";
					break;
				case '7':
					temp+="柒";
					break;
				case '8':
					temp+="捌";
					break;
				case '9':
					temp+="玖";
					break;
				case '0':
					temp+="零";
					break;
				}
				
			}
			return temp;
		}
	
	@Test
	public void test(){
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("请输入第一个参数数字是0--9");
			int s1 = sc.nextInt();
			System.out.println("请输入第二个参数数字是0--9");
			int s2 = sc.nextInt();
			System.out.println("请输入+,-,*,/");
			String s3 = sc.next();
			String calc = calc(s1,s2,s3);
			System.out.println(calc);
			
			String calcValue = getCalcValue(calc);
			System.out.println(calcValue);
		}
	}
}


你可能感兴趣的:(自己编写的计算器)