java 板


LINE

你好: 各位大大,我是新手,想请问各位几个问题: import java.util.Scanner; public class test15 { private static Scanner sc; public static void main(String[] args) { // 接收控制台输入的数字 System.out.println(numberProcess(getInt())); } /** * 安全的接受一个整数 * * @return 一个整数 */ public static int getInt() { int input = 0; sc = new Scanner(System.in); System.out.println("请输入一个整数,按Enter键结束:"); try { input = sc.nextInt(); } catch (Exception e) { input = getInt(); } return input; } public static String numberProcess(int number) { String inputProcess = String.valueOf(number); StringBuilder input = new StringBuilder(inputProcess); input.reverse(); StringBuilder outTemp = new StringBuilder(); char c; for (int i = 0; i < input.length(); i++) { c = input.charAt(i); switch (i + 1) { case 1: outTemp.append(dealWithChar(c)); break; case 2: outTemp.append('十'); outTemp.append(dealWithChar(c)); break; case 3: outTemp.append('百'); outTemp.append(dealWithChar(c)); break; case 4: outTemp.append('千'); outTemp.append(dealWithChar(c)); break; case 5: outTemp.append('万'); outTemp.append(dealWithChar(c)); break; case 6: outTemp.append('十'); outTemp.append(dealWithChar(c)); break; case 7: outTemp.append('百'); outTemp.append(dealWithChar(c)); break; case 8: outTemp.append('千'); outTemp.append(dealWithChar(c)); break; case 9: outTemp.append('亿'); outTemp.append(dealWithChar(c)); break; case 10: outTemp.append('十'); outTemp.append(dealWithChar(c)); break; } } outTemp = outTemp.reverse(); StringBuilder output = new StringBuilder(); for (int i = 0; i < outTemp.length() - 1; i++) { int j = i; char ch = outTemp.charAt(i); int len = outTemp.length() - i; if (len >= 9 && len <= 15) { if (ch == '零' && len == 9) { output.append("万"); } } if (len > 15) { if (ch == '零' && len == 15) { output.append("亿"); } } if (ch != '零') { output.append(ch); } else { char chs = outTemp.charAt(j); while (chs == '零' && j < outTemp.length() - 1) { j = j + 2; chs = outTemp.charAt(j); } output.append('零'); i = j - 1; } } char cha = outTemp.charAt(outTemp.length() - 1); if (cha != '零') { output.append(cha); } else { output.deleteCharAt(output.length() - 1); } return output.toString(); } public static char dealWithChar(char c) { switch (c) { case '0': return '零'; case '1': return '一'; case '2': return '二'; case '3': return '三'; case '4': return '四'; case '5': return '五'; case '6': return '六'; case '7': return '七'; case '8': return '八'; case '9': return '九'; } return ' '; } } 这是我写的程式码 遇到的问题有两点: 1.在整数的时候 无法显示中文数字 例如 :10→显示是一 无法显示十 20→显示是二 而不是二十 2.再输入10~19的数字的 时候 转换的中文数字前面都多个一字 例如:15→ 一十五 而不是十五 其他数字都 ok 只有10~19显示的有问题 烦请解惑 谢谢............................ --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.32.49.20
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1438845620.A.E2F.html
1F:→ cowbaying: reverse把0给去掉? 08/06 17:05
2F:→ samchen0119: 可否解惑 08/06 18:59
3F:推 DumDumGenius: reverse字串应该不会把0去掉 我猜是最後面 08/06 23:42
4F:→ DumDumGenius: 判断第一个数字是不是零那个地方 被删掉了 08/06 23:42
5F:推 DumDumGenius: 多塞一点 system.out.print trace一下巴 08/06 23:44
6F:推 k47100014: 你在前面的input.reverser(); 少了input = 08/08 19:06
7F:→ k47100014: 你那样写有写跟没写一样 @@" 08/08 19:07
8F:→ k47100014: input = input.reverse(); 08/08 19:07
9F:→ k47100014: 哦抱歉 可以那样写QQ 08/08 19:09







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP