今天用Eclipse编了个小程序,功能是将输入的金额转换成大写表示,比如我们转账的时候,输入1000,会提示“壹仟元整”。
程序包括一个class,里面有4个函数:
convert()--主转换函数
getInteger()--获取输入数字的整数部分
getDecimal()--获取输入数字的小数部分
numReplace()--替换掉尾部的0的数
代码如下:
package convert;
import java.text.DecimalFormat;
import java.util.Scanner;
public class MoneyConvert {
public static final String STR_UNIT[] = { "", "拾", "佰", "仟", "万", "拾",
"佰", "仟", "亿", "拾", "佰", "仟" };
public static final String STR_NUMBER[] = { "零", "壹", "贰", "叁", "肆", "伍",
"陆", "柒", "捌", "玖" };
public static final String STR_UNIT1[] = { "角", "分", "厘" };
public static void main(String[] args) {
try {
Scanner scan = new Scanner(System.in);
System.out.println("请输入转账金额:");
String convert = convert(scan.nextDouble());
System.out.println("请确认金额:" + convert);
scan.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String convert(double d) {
DecimalFormat df = new DecimalFormat("#0.###");
String strNum = df.format(d);
if(strNum.indexOf(".") != -1) { //-1 means strNum has no ".".
String num = strNum.substring(0, strNum.indexOf("."));//取整数部分
if(num.length() > 12) {
System.out.println("金额大于千亿,不能转账!");
System.exit(0);
}
}
if(strNum.indexOf(".") == -1) {
String num = strNum.substring(0);
if(num.length() > 12) {
System.out.println("金额大于千亿,不能转账!");
System.exit(0);
}
}
String point = "";
if(strNum.indexOf(".") != -1) {
point = "元";
}else {
point = "元整";
}
String result = getInterger(strNum) + point + getDecimal(strNum);
if(result.startsWith("元")) {
result = result.substring(1, result.length());
}
return result;
}
public static String getInterger(String num) {
if (num.indexOf(".") != -1) {
num = num.substring(0, num.indexOf("."));
}
num = new StringBuffer(num).reverse().toString();
StringBuffer temp = new StringBuffer();
for(int i = 0; i < num.length(); i++) {
temp.append(STR_UNIT[i]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
}
num = temp.reverse().toString(); // 反转字符串
num = numReplace(num, "零拾", "零"); // 替换字符串的字符
num = numReplace(num, "零佰", "零"); // 替换字符串的字符
num = numReplace(num, "零仟", "零"); // 替换字符串的字符
num = numReplace(num, "零万", "万"); // 替换字符串的字符
num = numReplace(num, "零亿", "亿"); // 替换字符串的字符
num = numReplace(num, "零零", "零"); // 替换字符串的字符
num = numReplace(num, "亿万", "亿"); // 替换字符串的字符
if(num.indexOf("零") == num.length() - 1) {
num = num.substring(0, num.length() - 1);
}
return num;
}
public static String getDecimal(String num) {
if (num.indexOf(".") == -1) {
return "";
}
num = num.substring(num.indexOf(".") + 1);//获取.之后的数字
num = new StringBuffer(num).toString();
StringBuffer temp = new StringBuffer();
for (int i = 0; i < num.length(); i++) {
temp.append(STR_NUMBER[num.charAt(i) - 48]);
temp.append(STR_UNIT1[i]);
}
num = temp.toString(); // 替换字符串的字符
num = numReplace(num, "零角", "零"); // 替换字符串的字符
num = numReplace(num, "零分", "零"); // 替换字符串的字符
num = numReplace(num, "零厘", "零"); // 替换字符串的字符
num = numReplace(num, "零零", "零"); // 替换字符串的字符
if (num.lastIndexOf("零") == num.length() - 1)
num = num.substring(0, num.length() -1);
return num;
}
public static String numReplace(String num, String oldStr, String newStr) {
while (true) {
if(num.indexOf(oldStr) == -1) break;
num = num.replaceAll(oldStr, newStr);
}
return num;
}
}
如果在Linux命令运行,有点需要注意,因为代码里有
package convert;
而我的编译文件又是放在了Eclipse默认的bin目录下,如下:
[root@sparkmaster bin]# ll convert/MoneyConvert.class
-rw-r--r--. 1 root root 4075 Feb 20 02:25 convert/MoneyConvert.class
[root@sparkmaster bin]# pwd
/root/workspace/Classic300/bin
所以,命令行里运行的时候,必须先去到class文件所在的包convert所在的目录,即MoneyConvert是类名,而这个类在convert这个package中,而MoneyConvert.class所在目录是/root/workspace/Classic300/bin/convert/MoneyConvert.class,所以我得进入/root/workspace/Classic300/bin去运行java convert/MoneyConvert才正确:
[root@sparkmaster bin]# java convert/MoneyConvert
请输入转账金额:
10000
请确认金额:壹万元整
否则,均会报如题所示错误:Exception in thread "main" java.lang.NoClassDefFoundError: MoneyConvert (wrong name: convert/MoneyConvert) 或者 Error: Could not find or load main class convert.MoneyConvert
后补
稍作修改之后,我把main函数单独写到一个文件里面,所以convert包括2个文件:MoneyConvert.java和RunConvert.java, 这样也算是更符合OOP吧
导出convert包为jar包之后,在Linux上运行:
[root@sparkmaster bin]# cd /opt/myjars/
[root@sparkmaster myjars]# ll
total 12
-rw-r--r--. 1 root root 3184 Feb 20 07:28 moneyconvert.jar
-rw-r--r--. 1 root root 6939 Jan 21 22:10 spark-wordcount-in-scala.jar
[root@sparkmaster myjars]# java -jar moneyconvert.jar
请输入转账金额:
10000
请确认金额:壹万元整
简单明了!