题目如下:
金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)->(一千零一拾一元整)输出。我写的程序如下:public class MoneyTransfer {
static String transfer(char c)
{
String ch = null;
switch(c){
case '1':
ch="一";
break;
case '2':
ch="二";
break;
case '3':
ch="三";
break;
case '4':
ch="四";
break;
case '5':
ch="五";
break;
case '6':
ch="六";
break;
case '7':
ch="七";
break;
case '8':
ch="八";
break;
case '9':
ch="九";
break;
}
return ch;
}
public static void main(String[] args)
{
String[] str={"千亿","百亿","十亿","亿","千万","百万","十 万 ","万","千","百","拾","元"};
char[] num = new char[12];
try {
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
String number = in.readLine();
char[] Cnumber = number.toCharArray();
for(int i=Cnumber.length-1,j=num.length-1;i>=0;i--,j--)
{
num[j]=Cnumber[i];
}
} catch (IOException e) {
e.printStackTrace();
}
int m=0;
while(num[m]== 0)
m++; for(int n=m;n<num.length;n++)
if(num[n] != '0')
System.out.print(transfer(num[n])+""+str[n]);
else
System.out.print("零");
}}但总是觉得写的太复杂,特别是用树组str保存单位那里觉得不是很好,大家有没有什么好的办法,一起来讨论学习啊
金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)->(一千零一拾一元整)输出。我写的程序如下:public class MoneyTransfer {
static String transfer(char c)
{
String ch = null;
switch(c){
case '1':
ch="一";
break;
case '2':
ch="二";
break;
case '3':
ch="三";
break;
case '4':
ch="四";
break;
case '5':
ch="五";
break;
case '6':
ch="六";
break;
case '7':
ch="七";
break;
case '8':
ch="八";
break;
case '9':
ch="九";
break;
}
return ch;
}
public static void main(String[] args)
{
String[] str={"千亿","百亿","十亿","亿","千万","百万","十 万 ","万","千","百","拾","元"};
char[] num = new char[12];
try {
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
String number = in.readLine();
char[] Cnumber = number.toCharArray();
for(int i=Cnumber.length-1,j=num.length-1;i>=0;i--,j--)
{
num[j]=Cnumber[i];
}
} catch (IOException e) {
e.printStackTrace();
}
int m=0;
while(num[m]== 0)
m++; for(int n=m;n<num.length;n++)
if(num[n] != '0')
System.out.print(transfer(num[n])+""+str[n]);
else
System.out.print("零");
}}但总是觉得写的太复杂,特别是用树组str保存单位那里觉得不是很好,大家有没有什么好的办法,一起来讨论学习啊
google之!
public class test{
int number[];
String bignum[] = {"零","一","二","三","四","五","六","七","八","久"};
String sign1[] = {"","十","百","千"};
String sign2[] = {"圆","万","亿","兆"};
test(String s){
s = s.replaceFirst("^0+","");
if(s.length()>sign2.length*4)
System.exit(0);
number = new int[s.length()];
for(int i=0;i<s.length();i++)
number[s.length()-1-i] = Integer.parseInt(i==s.length()-1?s.substring(i):s.substring(i,i+1));
run();
}
void run(){
String s="";
for(int i=number.length-1;i>=0;i--){
s+=bignum[number[i]];
s+=i%4==0?sign2[i/4]:number[i]>0?sign1[i%4]:"";
}
while(s.indexOf("零零")!=-1)
s=s.replaceFirst("零零","零");
for(int i=0;i<sign1.length;i++)
while(s.indexOf("零"+sign2[i])!=-1)
s=s.replaceFirst("零"+sign2[i],sign2[i]);
for(int i=sign2.length-1;i>0;i--)
while(s.indexOf(sign2[i]+sign2[i-1])!=-1)
s=s.replaceFirst(sign2[i]+sign2[i-1],sign2[i]);
System.out.println(s);
}
public static void main(String args[]){
new test(args[0]);
}
}
第19讲,Chain Of Responsibility 职责链模式(行为型模式
里面专门讲这个题目了。
虽然是用C#来讲的,但是用java也差不多一样实现。
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication44
{
class Program
{
static string transfer(string str)
{
string ChnNames = "零一二三四五六七八九";
string NumNames = "0123456789";
return Convert.ToString(ChnNames[NumNames.IndexOf(str)]);
}
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字:");
string str = Console.ReadLine();
int weishu = str.Length;
String[] danwei ={ "元", "拾", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
Console.WriteLine("转换后的结果是:");
int index = 0;
string output = "";
//生成转换后的结果
for (int i = str.Length; i > 0; i--)
{
index = str.Length - i;
output = output + transfer(str.Substring(index, 1));
if (str.Substring(index, 1) != "0" || i == 5 || i == 9 )
output = output + danwei[i - 1];
}
//把中间有2处或者2处以上连续0的地方给删除了
output = System.Text.RegularExpressions.Regex.Replace(output,@"(.)\1+", "$1");
//万和亿前面的“零”去除
for (int j = 0; j < output.Length - 1;j++ )
{
if (output.Substring(j, 1) == "零" && (output.Substring(j + 1, 1) == "万" || output.Substring(j + 1, 1) == "亿"))
output = output.Remove(j, 1);
}
//最末尾的“零”去除,并加上“元整”
if (output.Substring(output.Length - 1, 1) == "零")
{
output = output.Remove(output.Length - 1, 1);
output = output + "元整";
}
Console.WriteLine(output);
Console.WriteLine();
}
}
}