通过命令行输入一个字符串,首先判断这些字符是否为数字。如果其中有字符不是数字,那么直接在命令行输出“用户输入的字符不都为数字,无法转换”,程序结束;如果用户输入的字符都为数字,那么将该数字换成中文的大写表示(用户每次最多输入不超过8个字符)
例如:输入: 1234567
输出:一百二十三万四千五百六十七
输入: 0001
输出: 一
输入: 1001
输出: 一 千零一
输入: 1100
输出:一千一百
输入: 10100
输出:一万零一百
输入: 0010
输出:十
输入: 00000001
输出 :一
输入: 10000001
输出: 一千万零一
输入: 100 00010
输出: 一千万零十
输入: 10000100
输出:一千万零一百
输入: 11000000
输出:一千一百万
输入: 10100000
输出:一千零十万
输入: 10010000
输出 :一千零一万
输入: 10001000
输出: 一千万零一千
输入: a00045
输出: 用户输入的字符不都为数,无法转换请问这个题怎么写?
例如:输入: 1234567
输出:一百二十三万四千五百六十七
输入: 0001
输出: 一
输入: 1001
输出: 一 千零一
输入: 1100
输出:一千一百
输入: 10100
输出:一万零一百
输入: 0010
输出:十
输入: 00000001
输出 :一
输入: 10000001
输出: 一千万零一
输入: 100 00010
输出: 一千万零十
输入: 10000100
输出:一千万零一百
输入: 11000000
输出:一千一百万
输入: 10100000
输出:一千零十万
输入: 10010000
输出 :一千零一万
输入: 10001000
输出: 一千万零一千
输入: a00045
输出: 用户输入的字符不都为数,无法转换请问这个题怎么写?
百度的
public class NumUtil {
static final String[] cnNum = {"零","一","二","三","四","五","六","七","八","九"};
static final String[][] cnUnit = {{"","十","百","千"}, {"万","憶","兆","吉"}}; public static void main(String[] args) throws Throwable {
//测试数据LZ可以通过Scanner sc = new Scanner(System.in)修改为用户输入的方式
String[] testNum = {"0001","1001","1100","10100","0010","00000001",
"10000001","100 00010","10000100","11000000",
"10100000","10010000","10001000","a00045"};
for (String s : testNum) { //打印测试数据
try {
System.out.println(toCnNum(s));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} public static String toCnNum(long num) throws Exception { //转换为中文数字
return toCnNum(String.valueOf(num));
} public static String toCnNum(String num) throws Exception { //转换为中文数字
if (num == null) {
throw new Exception (String.format("illegal number[%s].", num));
} String chkNum = num.replaceAll("^[0]*(.*?)", "$1").replaceAll("\\s+", "").replaceAll(",", "");
if (!chkNum.matches("\\d+")) {
throw new Exception (String.format("illegal number[%s].", num));
}
int len = chkNum.length()%4 == 0 ? chkNum.length()/4 : chkNum.length()/4+1;
if (len > 4) {throw new Exception (String.format("number is too big[%s].", num));} String[] group = new String[len];
for (int i=chkNum.length()-4, j=len-1; j>=0; i-=4, j--) {
group[j] = chkNum.substring(Math.max(0, i), i+4);
}
//System.out.println(Arrays.toString(group)); StringBuilder buf = new StringBuilder();
for (int i=0; i<len; i++) {
int glen = group[i].length();
for (int j=0, k=0; j<glen; j++) {
char c = group[i].charAt(j);
if (c > '0') {
if (j==0 && i > 0 && group[i-1].charAt(3) == '0') {
buf.append(cnNum[0]);
} else if (k < j) {
buf.append(cnNum[0]);
}
if (j==2 && c=='1') { //这里是根据LZ的一十输出为十而做的条件判断
buf.append(cnUnit[0][glen-j-1]);
} else { //把if去掉,只要这里的else的代码就会变为一十的输出
buf.append(cnNum[(int)(c-'0')]).append(cnUnit[0][glen-j-1]);
}
k++;
}
} if (i < len-1 && Integer.valueOf(group[i]) > 0) {
buf.append(cnUnit[1][len-2-i]);
}
} return buf.toString();
}
}
if (j==0 && i > 0 && group[i-1].charAt(3) == '0') {
改成
if (j==0 && i > 0 && group[i-1].charAt(group[i-1].length()-1) == '0') {
就可以了import java.util.*;
public class NumUtil {
static final String[] cnNum = {"零","一","二","三","四","五","六","七","八","九"};
static final String[][] cnUnit = {{"","十","百","千"}, {"万","憶","兆","吉"}};
public static void main(String[] args) throws Throwable {
System.out.println(toCnNum("1234567")); //测试
} public static String toCnNum(long num) throws Exception {
return toCnNum(String.valueOf(num));
} public static String toCnNum(String num) throws Exception {
if (num == null) {
throw new Exception (String.format("illegal number[%s].", num));
} String chkNum = num.replaceAll("^[0]*(.*?)", "$1").replaceAll("\\s+", "").replaceAll(",", "");
if (!chkNum.matches("\\d+")) {
throw new Exception (String.format("illegal number[%s].", num));
}
int len = chkNum.length()%4 == 0 ? chkNum.length()/4 : chkNum.length()/4+1;
if (len > 4) {throw new Exception (String.format("parameter is too big[%s]", num));} String[] group = new String[len];
for (int i=chkNum.length()-4, j=len-1; j>=0; i-=4, j--) {
group[j] = chkNum.substring(Math.max(0, i), i+4);
}
//System.out.println(Arrays.toString(group)); StringBuilder buf = new StringBuilder();
for (int i=0; i<len; i++) {
int glen = group[i].length();
for (int j=0, k=0; j<glen; j++) {
char c = group[i].charAt(j);
if (c > '0') {
if (j==0 && i > 0 &&
group[i-1].charAt(group[i-1].length()-1) == '0') {//修改这里
buf.append(cnNum[0]);
} else if (k < j) {
buf.append(cnNum[0]);
}
if (j==2 && c=='1') {
buf.append(cnUnit[0][glen-j-1]);
} else {
buf.append(cnNum[(int)(c-'0')]).append(cnUnit[0][glen-j-1]);
}
k++;
}
} if (i < len-1 && Integer.valueOf(group[i]) > 0) {
buf.append(cnUnit[1][len-2-i]);
}
}
return buf.toString();
}
}
本来想拆成万以上和万以下可能会方便点,结果后面为了“零”字多了不少判断,目前测起来是正常的~~LZ看看吧public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
try {
Integer number = Integer.valueOf(str);
//最多只有8位,拆成万以上和万以下
int firstNum = number%10000;
int secondNum = number/10000;
String firstWord = getWordsByNum(firstNum);
String secondWord = getWordsByNum(secondNum);
StringBuffer sb = new StringBuffer();
sb.append(secondWord.startsWith("零")?secondWord.substring(1):secondWord);
sb.append(secondNum%10 == 0&&secondNum>0&&firstNum/1000>0? "零":"");
sb.append(secondWord.length()>0?"万"+firstWord:firstWord.startsWith("零")?firstWord.substring(1):firstWord);
System.err.println(sb.toString());
} catch (NumberFormatException e) {
System.err.println("用户输入的字符不都为数,无法转换");
}
}
} public static String getWordsByNum(int num) {
String num1 = getSingleWordByNum(num/1000);
String num2 = getSingleWordByNum(num%1000/100);
String num3 = getSingleWordByNum(num%100/10);
String num4 = getSingleWordByNum(num%10);
StringBuffer sb = new StringBuffer();
sb.append("零".equals(num1)?"零".equals(num2)?"":num1:num1+"千");
sb.append("零".equals(num2)?"零".equals(num3)?"":num2:num2+"百");
sb.append("零".equals(num3)?"零".equals(num4)?"":num3:"一".equals(num3)?"十":num3+"十");
sb.append("零".equals(num4)?"":num4);
return sb.toString();
}
public static String getSingleWordByNum(int num){
if(num == 1) return "一";
if(num == 2) return "二";
if(num == 3) return "三";
if(num == 4) return "四";
if(num == 5) return "五";
if(num == 6) return "六";
if(num == 7) return "七";
if(num == 8) return "八";
if(num == 9) return "九";
if(num == 0) return "零";
return "";
}
}
string[] str1 = new string[str.Length];
for (int i = 0; i < str.Length; i++)
{
str1[i] = ShuZu(str.Substring(i, 1));
Console.Write("{0}", str1[i]);
}
Console.WriteLine(); int y = (str.Length - 1) / 4; if (str.Length > 0 && str.Length <= 11)
{
for (int i = 0; i < str.Length; i++)
{ Console.Write("{0}", str1[i]);
if ((str.Length - 1 - i - 4 * y) != 0)
{
Console.Write("{0}", DanWei((str.Length - 1 - i - 4 * y) % 4));
} if (str.Length > 4)
{
if ((str.Length - i - 1) != 0 && (str.Length - i - 1) % 8 == 0)
{
Console.Write("亿");
}
else
if ((str.Length - i - 1) != 0 && (str.Length - i - 1) % 4 == 0)
{
Console.Write("万");
}
y -= 1;
} }
}
Console.Read();
} static string ShuZu(string n)
{
string str = "";
switch (n)
{
case "0": str = "零";
break;
case "1": str = "一";
break;
case "2": str = "二";
break;
case "3": str = "三";
break;
case "4": str = "四";
break;
case "5": str = "五";
break;
case "6": str = "六";
break;
case "7": str = "七";
break;
case "8": str = "八";
break;
case "9": str = "九";
break;
default:
break;
}
return str;
} static string DanWei(int n)
{
string str = " ";
switch (n)
{
case 1: str = "十";
break;
case 2: str = "百";
break;
case 3: str = "千";
break;
default:
break;
}
if (str == " ")
str = "";
return str;
}