- import java.util.Scanner;
- public class Test1 {
- public static void main(String[] args) {
- char[] chs = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
- while (true) {
- System.out.print("输入数字:");
- Scanner sc = new Scanner(System.in);
- try {
- int in = Integer.parseInt(sc.next());
- char[] des = (in + "").toCharArray();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < des.length; i++) {
- sb = sb.append(chs[(des[i] - 48)]);
- }
- System.out.println(sb);
- } catch (NumberFormatException e) {
- throw new RuntimeException("非纯数字或数字过大");
- }
- }
- }
- }
复制代码
思路就是把你第3步的思路,switch改为char数组,从数组中取对应中文字符 |