黑马程序员技术交流社区
标题:
使用Map集合解安卓35期基础班点招题数字转大写
[打印本页]
作者:
czw0916
时间:
2016-9-23 12:33
标题:
使用Map集合解安卓35期基础班点招题数字转大写
public class Demo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(method(sc.nextInt()));
}
public static String method(int num) {
String s = String.valueOf(num);
HashMap<Character, String> hs = new HashMap<>();
hs.put('0', "零");
hs.put('1', "一");
hs.put('2', "二");
hs.put('3', "三");
hs.put('4', "四");
hs.put('5', "五");
hs.put('6', "六");
hs.put('7', "七");
hs.put('8', "八");
hs.put('9', "九");
HashMap<Integer, String> h2 = new HashMap<>();
h2.put(1, "");
h2.put(2, "十");
h2.put(3, "百");
h2.put(4, "千");
h2.put(5, "万");
h2.put(6, "十万");
h2.put(7, "百万");
h2.put(8, "千万");
h2.put(9, "亿");
StringBuffer sb = new StringBuffer();
for (int i = s.length() - 1; i >= 0; i--) {
sb.append(h2.get(s.length() - i));
sb.append(hs.get(s.charAt(i)));
}
sb.reverse();
return sb.toString();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2