题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
我感觉没啥好说的,就是挺乱的,耐心点慢慢写
import java.util.*; public class Main { //准备 static List<String> list = Arrays.asList("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); static boolean isBilllion = false; static boolean isMillon = false; public static void main(String[] args) { Scanner scan = new Scanner(System.in); String input = scan.nextLine(); String before = input.split("\\.")[0]; String after = input.split("\\.")[1]; long beforeNum = Long.parseLong(before); StringBuilder sb = new StringBuilder("人民币"); //判断小数点之前 if (beforeNum / 100000000 != 0) { isBilllion = true; sb.append(output(0, (int) beforeNum / 100000000)).append("亿"); } if (beforeNum % 100000000 / 10000 != 0) { isMillon = true; sb.append(output(1, (int)beforeNum % 100000000 / 10000)).append("万"); } if (beforeNum % 10000 != 0) { sb.append(output(2, (int)beforeNum % 100000000 % 10000)).append("元"); } //判断小数点之后 if (after.equals("00")) { sb.append("整"); } else { int index1 = after.charAt(0) - '0'; if (index1 > 0) sb.append(list.get(index1)).append("角"); int index2 = after.charAt(1) - '0'; if (index2 > 0) sb.append(list.get(index2)).append("分"); } //输出 System.out.println(sb.toString()); } public static String output(int flag, int input) { String res = ""; int x = 1000; while (x > 0) { int quotient = input / x; if (quotient == 0) { if (x == 1000) { if (flag == 0) res += ""; if (flag == 1) res += isBilllion ? "零" : ""; } else { if (res.equals("零")) { res += ""; } else if (!res.equals("")) { res += "零"; } } } else if (quotient > 0) { String tail = x == 1000 ? "仟" : (x == 100 ? "佰" : (x == 10 ? "拾" : "")); if (quotient == 1 && x == 10) { res += "拾"; } else { res += list.get(quotient) + tail; } } input = input % x; x /= 10; } // if (input / 1000 == 0) { // if (flag == 0) res += ""; // if (flag == 1 && isBilllion) { // res += "零"; // } else if (flag == 1) { // res += ""; // } // } else if (input / 1000 > 0) { // res += list.get((int)input / 1000) + "仟"; // } // if (input % 1000 / 100 == 0 ) { // if (res.equals("零")) { // res += ""; // } else if (!res.equals("")) { // res += "零"; // } // } else if (input % 1000 / 100 > 0) { // res += list.get((int)input % 1000 / 100) + "佰"; // } // if (input % 100 / 10 == 0 ) { // if (res.equals("零")) { // res += ""; // } else if (!res.equals("")) { // res += "零"; // } // } else if (input % 100 / 10 > 0) { // if (input % 100 / 10 == 1) { // res += "拾"; // } else { // res += list.get((int)input % 100 / 10) + "拾"; // } // } // if (input % 10 == 0 ) { // if (res.equals("零")) { // res += ""; // } else if (!res.equals("")) { // res += "零"; // } // } else if (input % 10 > 0) { // res += list.get((int)input % 10); // } return res; } }