|
发表于 2020-4-24 09:30:01
|
显示全部楼层
我也写出来了,有兴趣测试一下啊.呵呵.
import java.io.*;
public class a
{
public static void main(String[] args)
{
String s=new String();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try{s=br.readLine();}catch(Exception e){}
Convert c = new Convert(s.toCharArray());
System.out.println(c);
}
}
}
class Convert
{
final String[] str1={"","十","百","千"};
final String[] str2 = {"亿","萬"};
final String[] number = {"","壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
char[] num_char;
StringBuffer sb=new StringBuffer();
public Convert(char[] num)
{
num_char=num;
}
public String toString()
{
int last=num_char.length%4;
int pos=0;
add(sb,new String(num_char,0,last));
pos+=last;
for(int i=(int) (num_char.length/4);i>0;i--)
{
if(i%2==0) sb.append(str2[0]);
else sb.append(str2[1]);
add(sb,new String(num_char,pos,4));
pos+=4;
}
if(last==0) sb.deleteCharAt(0);
return sb.toString();
}
private void add(StringBuffer sb,String temp)
{
char[] temp2=temp.toCharArray();
int length=temp.length();
for(int i=0;i<length;i++)
{
if(temp2[i]=='0')
if(sb.lastIndexOf("零")!=sb.length()-1 && i!=0) sb.append("零");else;
else
{
sb.append(number[temp2[i]-'0']);
sb.append(str1[length-i-1]);
}
}
try{
if (sb.lastIndexOf("零") == sb.length() - 1) sb.deleteCharAt(sb.lastIndexOf("零"));
}catch(Exception e){}
}
}
|
|