A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wenyu 中级黑马   /  2015-2-10 19:08  /  1423 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 wenyu 于 2015-2-11 16:53 编辑

4、        从键盘接受一个数字,列出该数字的中文表示格式,例如:键盘输入123,打印出一二三;键盘输入3103,打印出三一零三。

import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
   
  Scanner sc = new Scanner(System.in);
  int x = sc.nextInt();
  
  trans(x);
  
  
}
public static void trans(int a){
  String [] str = {"零","一","二","三","四","五","六","七","八","九"};
  int y = a%10;
  a /=10;
  if(a>0)
   trans(a);
  System.out.print(str[y]);
  
}   
}
按照上面的代码可以出效果,但感觉递归内存占用率会很大,不知道还有什么方法优化一下?

6 个回复

倒序浏览
String s = sc.nextLine();输入一行字符串,这样处理就方便了
回复 使用道具 举报
简单!
  1. package com.itheima;

  2. import java.util.Scanner;
  3. public class Test1 {

  4.        
  5.         public static void main(String[] args) {
  6.           
  7.           Scanner sc = new Scanner(System.in);
  8.           int x = sc.nextInt();
  9.           
  10.           trans(x);
  11.           
  12.         }
  13.        
  14.         public static void trans(int a){
  15.           String [] str = {"零","一","二","三","四","五","六","七","八","九"};
  16.          /* int y = a%10;
  17.           a /=10;
  18.           if(a>0)
  19.            trans(a);
  20.           System.out.print(str[y]);*/
  21.           StringBuilder sb=new StringBuilder();
  22.           int y=0;
  23.           while(a>0){
  24.                   y = a%10;
  25.                   sb.append(str[y]);
  26.                   a=a/10;
  27.           }
  28.           System.out.println(sb.reverse().toString());
  29.         }   

  30. }
复制代码
回复 使用道具 举报
以前也做过类似的代码
  1. // 从键盘接受一个数字,列出该数字的中文表示格式,例如:键盘输入123,打印出一二三;键盘输入3103,打印出三一零三。
  2.         public static void main(String[] args) {
  3.                 try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
  4.                         System.out.print(">>");
  5.                         String string = bufferedReader.readLine();
  6.                         String[] numbers = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
  7.                         for (int i = 0; i < string.length(); i++) {
  8.                                 System.out.print(numbers[new Integer(new Character( string.charAt(i)).toString())]);
  9.                         }
  10.                 } catch (Exception e) {
  11.                         e.printStackTrace();
  12.                 }
  13.         }
复制代码
回复 使用道具 举报
package NetIOTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class BaseTest {
        public static void main(String [] args)throws IOException{
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
                String line = "";
                while ((line=bufr.readLine())!=null){
                        toChinese(line);
                       
                }
        }
        public static void toChinese(String str){
                StringBuilder sb = new StringBuilder();
                char [] c = {'零','一','二','三','四','五','六','七','八','九'};
                char [] chs = str.toCharArray();
                for(int index=0;index<str.length();index++){
                        int x = chs[index]-48;
                        sb.append(c[x]);
                }
                System.out.println(sb.toString());
        }
}


经典的查表法
回复 使用道具 举报
我也练一下手
  1. char[] list = {'零','一','二','三','四','五','六','七','八','九',};

  2. BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

  3. String str = bufr.readLine();

  4. for (int i=0; i<str.length(); i++) {
  5.     int index = str.charAt(i) - '0';
  6.     System.out.print(list[index]);
  7. }
复制代码
回复 使用道具 举报
Ansel 中级黑马 2015-2-17 00:32:42
7#
恩 楼上都是我小号
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马