import java.util.*;
public class Main {
static char[] upper = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 大写数字
// 获取一个四位数的大写字符串,零的数目可以为多个
static String addZero(String t) {
StringBuilder sb = new StringBuilder();
int n1 = Integer.parseInt("" + t.charAt(0));
int n2 = Integer.parseInt("" + t.charAt(1));
int n3 = Integer.parseInt("" + t.charAt(2));
int n4 = Integer.parseInt("" + t.charAt(3));
if (n1 == 0) {
sb.append("零");
} else {
sb.append(upper[n1] + "仟");
}
if (n2 == 0) {
sb.append("零");
} else {
sb.append(upper[n2] + "佰");
}
if (n3 == 0) {
sb.append("零");
} else {
sb.append(upper[n3] + "拾");
}
if (n4 != 0) {
sb.append(upper[n4]);
}
return sb.toString();
}
static void f(String num) {
int len = num.length();
if (len < 5) {
for (int i = 0; i < 4 - len; i++) {
num = "0" + num;
}
String s = addZero(num);
// 去尾部的零
while (s.endsWith("零")) {
s = s.substring(0, s.length() - 1);
}
// 去首部的零
while (s.startsWith("零")) {
s = s.substring(1);
}
// 消除重复的零
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
System.out.println(s);
} else if (len < 9) {
String s, s1, s2;
s1 = num.substring(0, num.length() - 4);
int i = 4 - s1.length();
while (i-- > 0) {
s1 = "0" + s1;
}
s1 = addZero(s1) + "万";
s2 = num.substring(num.length() - 4);
s2 = addZero(s2);
s = s1 + s2;
while (s.endsWith("零")) {
s = s.substring(0, s.length() - 1);
}
while (s.startsWith("零")) {
s = s.substring(1);
}
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
System.out.println(s);
} else {
String s, s1, s2, s3;
s1 = num.substring(0, num.length() - 8);
int i = 4 - s1.length();
while (i-- > 0) {
s1 = "0" + s1;
}
s2 = num.substring(num.length() - 8, num.length() - 4);
s3 = num.substring(num.length() - 4);
s1 = addZero(s1) + "亿";
s2 = addZero(s2) + "万";
s3 = addZero(s3);
s = s1 + s2 + s3;
while (s.endsWith("零")) {
s = s.substring(0, s.length() - 1);
}
while (s.startsWith("零")) {
s = s.substring(1);
}
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
System.out.println(s);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String num = scan.next();
scan.close();
f(num);
}
}
当你把你的代码缕缕至少不是那么的乱,像这样,别人怎么看你的代码???
当时太急 也没看 嘻嘻 后来自己就弄好了 谢谢哟
#include<stdio.h>
void main()
{
double r=0;
int d=0;
int x,n,j,i,k,flag=0;
char str1[10][10]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
char str[10][10]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
char str2[14][10]={"拾","亿","仟","佰","拾","萬","仟","佰","拾",""};
char str3[30];
char str4[30];
char getstr[30];
printf("请输入金额(范围小于十亿):\n");
gets(getstr);
j=0;
for(i=0;getstr[i]!='\0';i++)
{
if(getstr[i]>='0'&&getstr[i]<='9')
str4[j++]=getstr[i];
}
str4[j]='\0';
puts(str4); //修整过后的数
for(i=0;str4[i]!='\0';i++)
{
if(str4[i]>'0')
{
d=(j-i);break;
}
}
if(d>=9)
printf("输入的数超出范围,请重新输入\n");
for(k=0;k<10-d;k++)
str3[k]='0';
for(k=10-d;k<=9;k++)
{str3[k]=str4[i];++i;}
// 重新赋值
n=10-d;
for(j=n;j<=9;j++)
{
if(str3[j]!='0')
{x=str3[j]-48; printf("%s",str1[x]);}
else if(str3[j]=='0')
{
flag=0;
if(j<=5)
{
for(k=j;k<=5;k++)
if(str3[k]!='0'){flag=1;j=k-1;break;}
if(flag==1)
printf("零");
}
else if(j>5)
{
for(k=j;k<=9;k++)
{
if(str3[k]!='0'){flag=1;j=k-1;break;}
}
if(flag==1)
printf("零");
}
}
if(str3[j]!='0'||j==1)
{ printf("%s",str2[j]);}
if(j==5&&str3[j]=='0'&&(str3[4]!='0'||str3[3]!='0'||str3[2]!='0'))
printf("%s",str2[5]);
}
printf("\n");
}