JAVA笔试题

来源:互联网 发布:软件系统实施计划 编辑:程序博客网 时间:2024/04/26 00:45

 

题目:要求从键盘输入一数字,然后中文输出(要符合中文语法)
例:12434   输出 一万二千四百三十四
 

解法一:

String num = "零壹贰叁肆伍陆柒捌玖";
    String dw = "圆拾佰仟万亿";
    String m = "30020.23";
    String mm[] = null;
    mm = m.split("//.");
    String money = mm[0];

    String result = num.charAt(Integer.parseInt("" + mm[1].charAt(0))) + "角" +
        num.charAt(Integer.parseInt("" + mm[1].charAt(1))) + "分";

    for (int i = 0; i < money.length(); i++) {
      String str = "";
      int n = Integer.parseInt(money.substring(money.length() - i - 1,
                                               money.length() - i));
      str = str + num.charAt(n);
      if (i == 0) {
        str = str + dw.charAt(i);
      }
      else if ( (i + 4) % 8 == 0) {
        str = str + dw.charAt(4);
      }
      else if (i % 8 == 0) {
        str = str + dw.charAt(5);
      }
      else {
        str = str + dw.charAt(i % 4);
      }
      result = str + result;
    }
    result = result.replaceAll("零([^圆]{1})", "零");
    result = result.replaceAll("零+", "零");
    result = result.replaceAll("零圆", "圆");
    System.out.println(result);

解法二:

接一楼的程序,帮你改了一下,顺便自己也学习了一下
public static String tochinessupper(String number)
{
String num = "零壹贰叁肆伍陆柒捌玖";
    String dw = "圆拾佰仟万亿";
    String m = number;
    String mm[] = null;
    String money=null;
    String result=null;
    if(m.indexOf(".")!=-1)
    {
    mm = m.split("//.");
    money = mm[0];
    result = num.charAt(Integer.parseInt("" + mm[1].charAt(0))) + "角" +num.charAt(Integer.parseInt("" + mm[1].charAt(1))) + "分";
    }
    else
    {
    money=m;
    result="";
    }
   
    for (int i = 0; i < money.length(); i++)
    {
      String str = "";
      int n = Integer.parseInt(money.substring(money.length() - i - 1,money.length() - i));
      str = str + num.charAt(n);
      if (i == 0)
      {
        str = str + dw.charAt(i);
      }
      else if ( (i + 4) % 8 == 0)
      {
        str = str + dw.charAt(4);
      }
      else if (i % 8 == 0)
      {
        str = str + dw.charAt(5);
      }
      else
      {
        str = str + dw.charAt(i % 4);
      }
      result = str + result;
    }
    result = result.replaceAll("零([^圆]{1})", "零");
    result = result.replaceAll("零+", "零");
    result = result.replaceAll("零圆", "圆");
    return result;
}

 

三、对解法一的改进!

还有BUG,再改下:
String num = "零壹贰叁肆伍陆柒捌玖";
    String dw = "圆拾佰仟万亿";
    String m = "30020.23";
    String mm[] = null;
    mm = m.split("//.");
    String money = mm[0];

    String result = num.charAt(Integer.parseInt("" + mm[1].charAt(0))) + "角" +
        num.charAt(Integer.parseInt("" + mm[1].charAt(1))) + "分";

    for (int i = 0; i < money.length(); i++) {
      String str = "";
      int n = Integer.parseInt(money.substring(money.length() - i - 1,
                                               money.length() - i));
      str = str + num.charAt(n);
      if (i == 0) {
        str = str + dw.charAt(i);
      }
      else if ( (i + 4) % 8 == 0) {
        str = str + dw.charAt(4);
      }
      else if (i % 8 == 0) {
        str = str + dw.charAt(5);
      }
      else {
        str = str + dw.charAt(i % 4);
      }
      result = str + result;
    }

    result = result.replaceAll("零([^亿万圆])", "零");
    result = result.replaceAll("亿零+万","亿零");
    result = result.replaceAll("零+", "零");
    result = result.replaceAll("零([亿万圆])", "$1");
    result =result.replaceAll("^壹拾","拾");

    System.out.println(result);

 

解法四:

import java.io.*;

public class Turn {

public static void main(String args[]){

Turn t = new Turn();
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String s=in.readLine();
t.print(s);
for(int i=0; i<=s.length()-1; i++){
t.diffract(Integer.parseInt(String.valueOf(s.charAt(i))));
t.unit(s.length()-i);
}
}
catch(Exception e){
e.printStackTrace();
}

}

public void diffract(int i){
switch(i){
case 1:print("一");break;
case 2:print("二");break;
case 3:print("三");break;
case 4:print("四");break;
case 5:print("五");break;
case 6:print("六");break;
case 7:print("七");break;
case 8:print("八");break;
case 9:print("九");break;
case 0:print("零");break;
}
}

public void unit(int i){
//打印单位
switch(i){
case 1:break;
case 2:print("十");break;
case 3:print("百");break;
case 4:print("千");break;
case 5:print("万");break;
case 6:print("十万");break;
case 7:print("百万");break;
}
}

public void print(String unit){
System.out.print(unit);
}
}

 

来自:http://community.csdn.net/Expert/topic/5093/5093849.xml?temp=6.193179E-02

关注中


原创粉丝点击