本文共 4000 字,大约阅读时间需要 13 分钟。
上周五例会结束了,打算做到练习题放松下,于是选择了这道(人民币大写转换),一看这道题目觉得很简单,默默告诉在半个小时一定能搞定。如果你一听这道题也觉得很简单,说明你和我是在思想认识上是在同一个高度。
整个题目意思是: 输入阿拉伯数字,然后翻译人民币大写。例如 1 1101 1101 壹忆壹仟壹佰零壹万 壹仟壹佰零壹
是不是很简单? 立马我分析设计:
1. 定义大写对应的常量字符串数组和个十百仟的单位常量。
2.把输入的数据字符串按照4,8进行分割,然后单独处理。
3. 处理分割后的字符串,4-8之间字符串加万,8以上的加亿
4. 对于连续的是零的进行单独处理
接下来就开始编程了。5分钟过去了,30分钟过去了,1个小时过去了...
被你猜对了,花了1半小时都没有搞定,最后放弃了,就回家了。搞得周五有点小郁闷。刚刚做了一下,算是搞定了。
package com.albertshao.study;import java.util.ArrayList;import java.util.List;/** * * 问题: 把阿拉伯数字转换为大写, * // 1. store the constant variables of number and unit //2. handle the number below the thousand. // 3. split the number per 4 space according to the length of number // 4. handle the special condition such as the continuous zero * @author albertshao * */public class RMBConverter { private static final String NUMBER_UPCASE = "零壹贰叁肆伍陆柒捌玖"; private static final String[] RMB_UNIT = {"分","角","元","拾","佰","仟","万","亿" }; private static final String NUNBER_REGEX = "^[0-9]*$"; /** * 第二个四位表示万 */ private static final Integer TENTHOUSAND_INDEX = 2; /** * 第三个四位表示亿 */ private static final Integer HUNDREDMILLION_INDEX = 3; /** * 第四个四位表示亿 */ private static final Integer MILLIONMILLION_INDEX = 4; public static void main(String args[]) { System.out.println("10021243:" + RMBConverter.convert(10021243)); System.out.println("1110201220012:" +RMBConverter.convert(1110201220012L)); } /** * Convert the input number to the expected result. * @param number * @return */ private static String convert(long number) { String result = ""; List测试结果numList = RMBConverter.splitNumberStr(String.valueOf(number)); if(!numList.isEmpty()) { int index = 1; for(String numStr : numList) { String temp = convertUpCase(numStr); if(index == TENTHOUSAND_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[6]); } else { temp += RMB_UNIT[6]; } } else if(index == HUNDREDMILLION_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[7]); } else { temp += RMB_UNIT[7]; } } else if(index == MILLIONMILLION_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[6]+RMB_UNIT[7]); } else { temp += (RMB_UNIT[6]+RMB_UNIT[7]); } } result = temp + result; index ++; } } return result; } /** * Convert upcase of number below thousand. * 1101 壹仟壹佰零壹元 * @param numberStr * @return */ private static String convertUpCase(String numberStr) { StringBuilder resSb = new StringBuilder(0); if (numberStr != null && !"".equals(numberStr)) { char[] arr = numberStr.toCharArray(); int count = arr.length; for (char ch : arr) { String charUpCase = String.valueOf(NUMBER_UPCASE .charAt(Character.getNumericValue(ch))); //4. handle the special condition such as including zero if (Character.getNumericValue(ch) == 0) { if(resSb.toString().endsWith("零")) { count--; continue; } else { resSb.append(charUpCase); } } else { resSb.append(charUpCase); resSb.append(RMB_UNIT[count + 1]); } count--; } } else { resSb.append(numberStr); } return resSb.toString(); } //3. split the number according to the length of number private static List splitNumberStr(String numberStr) { List numList = new ArrayList (); if (numberStr != null && !"".equals(numberStr) && numberStr.matches(NUNBER_REGEX)) { while (numberStr.length() > 4) { String numCell = numberStr.substring(numberStr.length() - 4); numList.add(numCell); numberStr = numberStr.substring(0, numberStr.length() - 4); } numList.add(numberStr); } return numList; }}
10021243:壹仟零贰万壹仟贰佰肆拾叁元1110201220012:壹万亿壹仟壹佰零贰亿零壹佰贰拾贰万零壹拾贰元失败总结:
1. 对于具体设计没有考虑清楚,做编程题时候一定要清楚具体实现细节,考虑实现的可行性。
2. 不要在烦躁的情况下做题,比如周五,大家都想回家,一个人在那边编写代码,心里却想着早点回家,这样的效率往往受影响。所以不要强迫自己编程。玩时玩得痛快,学时学得踏实。
转载地址:http://rskai.baihongyu.com/