java数字转化为对应中文
功能要求:输入一个由数字组成的字符串,输出对应的中文表达式。如545 : 五百四十五
Ps:功能可能还不够完善,欢迎提出宝贵意见。
注:程序只能计算0-9999 9999 9999的数字范围,如果需要增加位数,可以对pptArr数组进行更改:
如:pptArr = {"",“万”,“亿”,“万”,“亿”} 范围0 - 9999 9999 9999 9999 9999 9999;
只需要增加 “万"和"亿” 元素即可扩展范围;
附上代码
package aki.chyan7.test;
import java.util.Scanner;
public class test {
private String [] hanArr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private String [] unitArr = {"十","百","千"};
private String [] pptArr = {"","万","亿"};
public static void main(String [] g){
test nr = new test();
String new_pptArrays = "";
System.out.println("输入金额(不包含小数):");
Scanner input = new Scanner(System.in);
String inputArr = input.next();
new_pptArrays = nr.dejudge(inputArr);//去除前导零
new_pptArrays = nr.divide(new_pptArrays);//整数划分 + 初步转化字符串并返回
new_pptArrays = nr.reload(new_pptArrays);//对初步转化的数组进行 合法性判断,进一步转化为合法结果
System.out.println(new_pptArrays);
}
private String dejudge(String result){//对输入的字符串重新定义:去除前导零
String newResult = "";
boolean flag = false;
for(int i=0;i<result.length();i++){
if(flag == false){
if(result.charAt(i)!='0'){
flag = true;
newResult = newResult + result.charAt(i);
}
}
else{
newResult = newResult + result.charAt(i);
// System.out.println("去除0后的字符串:"+newResult);
}
}
return newResult;
}
private String reload(String numStr){
String result = numStr;
String new_result = "";
for(int i=0;i<result.length();i++){
if(result.charAt(i)=='零'){//如果要输出当前的 零 ,必须满足紧跟 零 后面的第一个字符不是 零、万、亿 这三种字符
if(i!=result.length()-1&&i!=0){//第一位和最后一位不需要判断
if(result.charAt(i+1)!='零'&&result.charAt(i+1)!='亿'&&result.charAt(i+1)!='万'){
new_result = new_result + result.charAt(i);
// System.out.println(new_result);
}
}
//隐含当最后一位为零时,不对字符串进行操作,如 50 不会输出 五十零 。
}
else{
if(result.charAt(i)=='万'){//如果要输出当前的 万 ,必须满足 万 前面的万位的四位数字不全为 零
if(result.charAt(i-1)!='零'||result.charAt(i-2)!='零'||result.charAt(i-3)!='零'||result.charAt(i-4)!='零'){
//&&result.charAt(i-1)!='亿'&&result.charAt(i-1)!='万'
new_result = new_result + result.charAt(i);
}
// if(result.charAt(i-1)=='零'&&result.charAt(i-2)=='零'&&result.charAt(i-3)=='零'&&result.charAt(i-4)=='零'){
// if(result.charAt(i+1)!='零'||result.charAt(i+2)!='零'||result.charAt(i+3)!='零'||result.charAt(i+4)!='零')
// new_result = new_result + "零";
// }
}
else{
// if(i==result.length()-1&&result.charAt(i+1)=='零')
// new_result = new_result;
// else
new_result = new_result + result.charAt(i);//其他情况直接改变字符串
// System.out.println(new_result);
}
}
}
return new_result;
}
private String divide(String numStr){//拆分字符串,并转化字符串
String new_result = "";
String middle = "";
String new_middle = "";
for(int i=numStr.length()-1,j=1;i>=0;i--,j++){//通过循环倒序划分,通过 j 来存储 当前的字符 的次序
middle += numStr.charAt(i);
if(j%4==0||i==0){//每四个字符为一组进行转化
test nr = new test();
for(int k = middle.length()-1;k>=0;k--){//划分的字符串是倒序,将倒序改为正序
new_middle = new_middle + middle.charAt(k);
}
System.out.println(new_middle);//输出划分的结果
if(j%4==0)//次序每4个单位转化一次,4的倍数需要 赋予相应的 ”“(无值)、”万“、”亿“;
new_result = nr.toHanStr(new_middle) + pptArr[j/4-1] + new_result;//进行字符串转化
// 转化后的字符串也是倒序转化,通过在后面加达到正序的效果
else//不足,4的倍数的同样需要 赋予相应的 ”“(无值)、”万“、”亿“;
new_result = nr.toHanStr(new_middle) + pptArr[j/4] + new_result;
middle = "";//初始化两个划分数组,方便下次划分;
new_middle = "";
}
}
return new_result;
}
//改变值
private String toHanStr(String numStr){//以1-4个字符为一单位进行转化,如9999 = 九千九百九十九
String result = "";
int numLen = numStr.length();
for(int i = 0;i<numLen;i++){
int num = numStr.charAt(i) - 48;
if(i!=numLen-1&&num!=0){
result = result + hanArr[num] + unitArr[numLen-i-2];
}
else{
result = result + hanArr[num];
}
}
return result;
}
}
以上就是全部代码,在代码中间有写一些注释。
有问题欢迎联系或留言,q:1476911135