1.写一个函数,讲数字转换成中文大写形式,如输入123456,转换成拾贰万叁千肆佰伍拾陆。
代码转自:http://blog.csdn.net/netfalcon/archive/2004/03/09/21645.aspx
代码如下:
2.讲括号进行匹配,括号不成对。如(12)
3.实现整型数组的排序和反转
代码转自:http://blog.csdn.net/netfalcon/archive/2004/03/09/21645.aspx
代码如下:
package cn.myTest;
import java.lang.String;
import java.lang.Math;
public class Change {
// 调试时用main函数
public static void main(String args[]) {
Change mon = new Change();
System.out.println(mon.NumToRMBStr(100000001.05));
}
String HanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
String HanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟",
"亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万",
"拾", "佰", "仟" };
String PositiveIntegerToHanStr(String NumStr) { // 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零
String RMBStr = "";
boolean lastzero = false;
boolean hasvalue = false; // 亿、万进位前有数值标记
int len, n;
len = NumStr.length();
if (len > 15)
return "数值过大!";
for (int i = len - 1; i >= 0; i--) {
if (NumStr.charAt(len - i - 1) == ' ')
continue;
n = NumStr.charAt(len - i - 1) - '0';
if (n < 0 || n > 9)
return "输入含非数字字符!";
if (n != 0) {
if (lastzero)
RMBStr += HanDigiStr[0]; // 若干零后若跟非零值,只显示一个零
// 除了亿万前的零不带到后面
// if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) //
// 如十进位前有零也不发壹音用此行
if (!(n == 1 && (i % 4) == 1 && i == len - 1)) // 十进位处于第一位不发壹音
RMBStr += HanDigiStr[n];
RMBStr += HanDiviStr[i]; // 非零值后加进位,个位为空
hasvalue = true; // 置万进位前有值标记
} else {
if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) // 亿万之间必须有非零值方显示万
RMBStr += HanDiviStr[i]; // “亿”或“万”
}
if (i % 8 == 0)
hasvalue = false; // 万进位前有值标记逢亿复位
lastzero = (n == 0) && (i % 4 != 0);
}
if (RMBStr.length() == 0)
return HanDigiStr[0]; // 输入空字符或"0",返回"零"
return RMBStr;
}
String NumToRMBStr(double val) {
String SignStr = "";
String TailStr = "";
long fraction, integer;
int jiao, fen;
if (val < 0) {
val = -val;
SignStr = "负";
}
if (val > 99999999999999.999 || val < -99999999999999.999)
return "数值位数过大!";
// 四舍五入到分
long temp = Math.round(val * 100);
integer = temp / 100;
fraction = temp % 100;
jiao = (int) fraction / 10;
fen = (int) fraction % 10;
if (jiao == 0 && fen == 0) {
TailStr = "整";
} else {
TailStr = HanDigiStr[jiao];
if (jiao != 0)
TailStr += "角";
if (integer == 0 && jiao == 0) // 零元后不写零几分
TailStr = "";
if (fen != 0)
TailStr += HanDigiStr[fen] + "分";
}
// 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”
// if( !integer ) return SignStr+TailStr;
return "¥" + SignStr + PositiveIntegerToHanStr(String.valueOf(integer))
+ "元" + TailStr;
}
}// END public class money
2.讲括号进行匹配,括号不成对。如(12)
3.实现整型数组的排序和反转
package com.bmwm5gtr.test;
public class ArrayClass{
/**
* 此方法将数组进行反转
* @param number
* @return
*/
public static String inverse(int[] number){
int temp=0;
int count=0;
for(int i=0;i<number.length;i++){
count=number.length-i-1;
if(i>=count)
break;
else{
temp=number[count];
number[count]=number[i];
number[i]=temp;
}
}
return printF(number,"inverse");
}
/**
* 对数组进行排序,从小到大和从大到小
* @param number
* @param operType
*/
public static String sort(int[] number,String operType){
int tem=0;
for(int i=0;i<number.length;i++){
for(int j=number.length-1;j>i;j--){
if("minToMax".equalsIgnoreCase(operType)){
if(number[i]>number[j]){
tem=number[i];
number[i]=number[j];
number[j]=tem;
}
}else{
if(number[i]<number[j]){
tem=number[i];
number[i]=number[j];
number[j]=tem;
}
}
}
}
return printF(number,operType);
}
/**
* 改方法负责打印数组
* @param number
* @return
*/
public static String printF(int[] number,String operType){
String cnName=("inverse".equalsIgnoreCase(operType))?"反转":("minToMax".equalsIgnoreCase(operType)?"从小到大排序":("maxToMin".equalsIgnoreCase(operType)?"从大到小排序":"初始化"));
cnName+="后:";
for(int x:number){
cnName+=x+"-->";
}
return cnName.substring(0, cnName.length()-3);
}
}
package com.bmwm5gtr.test;
public class ArrayDemo{
public static void main(String args[]){
int[] number={47,10,12,89,1,87,15,43,22};
System.out.println(ArrayClass.printF(number,"normal"));
System.out.println(ArrayClass.inverse(number));
System.out.println(ArrayClass.sort(number,"minToMax"));
System.out.println(ArrayClass.sort(number,"maxToMin"));
}
};