华为机试:中文整数读法的拼音转数字


import java.util.*;

public class Transfer {
	public static void main(String[] args) {
		Map<Integer, Integer> mapint = new HashMap<Integer, Integer>();
		int count = 0;
		int sum = 0; //calculate result
		Map<String, Integer> map = new HashMap<String, Integer>() {
			{
				put("Ling", 0);
				put("Yi", 1);
				put("Er", 2);
				put("San", 3);
				put("Si", 4);
				put("Wu", 5);
				put("Liu", 6);
				put("Qi", 7);
				put("Ba", 8);
				put("Jiu", 9);
				put("Shi", 10);
				put("Bai", 100);
				put("Qian", 1000);
				put("Wan", 10000);
				put("Yii", 100000000);
			}
		};
		Scanner in = new Scanner(System.in);
		System.out.println("请输入:");
		String line = in.nextLine();
		// 赋值
		for (int i = 0; i < line.length();) {
			String str1 = "";
			str1 = line.substring(i, i + 1);
			String str2toKey = "";
			char[] chars = new char[4];
			chars[0] = str1.charAt(0);
			i++;//add here
			if (chars[0] >= 'A' && chars[0] <= 'Z') {
				int j = 0;
				while (j < 3 && (line.substring(i, i + 1)).charAt(0) >= 'a'
						&& (line.substring(i, i + 1)).charAt(0) <= 'z') {
					chars[++j] = (line.substring(i, i + 1)).charAt(0);
					i++;
					if (i == line.length()){  //add a guard, or will exception
						break;
					}
				}
				// 判断 chars是否是大写
				str2toKey= String.valueOf(chars); // 保存一串字母
				mapint.put(count++, map.get(str2toKey.trim()));// 保存对应的数字
				//System.out.println(mapint.get(count-1));
			}
		}
		for(int i=0;i<mapint.size();i=i+2){
			if(mapint.get(i) == 0){
				sum+=mapint.get(i+1);
			}
			else {
				if(i+1 != mapint.size()){
					sum=sum+mapint.get(i)*mapint.get(i+1);	
				}
				else{
					sum+=mapint.get(i);
				}	
			}
		}
		System.out.println("转换后: \n"+sum);
	}
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值