我原来写过差不多的,你看看吧。自己改改就能使了。不要只为了完成作业。。。。
import java.io.*;
public class MoneyTest
{
public MoneyTest()throws IOException
{
String shu[] = {"零","壹","贰","叁","肆","伍","陆","柒","玐","玖"};
String wei[] = {"十万","万","仟","佰","拾","","","角","分"};
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("0推出\n请输入:");
String s = "";
while(!(s = bfr.readLine()).equals("0"))
{
try{
double d = Double.parseDouble(s);
if(d>100000||d<0)
{
throw new IOException();
}
int point = s.indexOf(".");
if(point != -1)
{
s = s+"00";
s = s.substring(0,point+3);
}
else
{
s = s+".00";
point = s.indexOf(".");
}
int flag = 0;
String result = "";
for(int i = 0;i<=s.length()-1;i++)
{
if(s.charAt(i)=='0')
{
flag = 1;
}
else if (s.charAt(i)!='0'&&s.charAt(i)!='.')
{
if(flag == 1)
{
result = result+"零";
}
result = result+shu[s.charAt(i)-'0']+wei[(6-point)+i];
flag = 0;
}
if(s.charAt(i)=='.')
{
result = result + "元";
}
}
if(result.charAt(result.length()-1)=='元')
{
result = result + "整";
}
System.out.println("结果为 "+result);
}
catch(Exception e)
{
System.out.println("输入数据格式只能为0-10万之间的数字");
}
System.out.println("请输入:");
}
}
public static void main(String args[])throws IOException
{
new MoneyTest();
}
}