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/m.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燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP