java编程 数字转换成汉字
需要利用java.util包中的Stack实现比如输入1234,则输出一千二百三十四谢谢高手们……需要接收用户自己输入的随意整数。别像一楼复制别人的,,估计他自己都没运行...
需要利用java.util包中的Stack实现
比如输入 1234, 则输出 一千二百三十四
谢谢高手们……
需要接收用户自己输入的随意整数 。
别像一楼 复制别人的,, 估计他自己都没运行过。 展开
比如输入 1234, 则输出 一千二百三十四
谢谢高手们……
需要接收用户自己输入的随意整数 。
别像一楼 复制别人的,, 估计他自己都没运行过。 展开
5个回答
展开全部
我自己写的,匆忙写的。我有时间再改进改进。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Stack;
/*
* 本程序
*/
public class Transfer {
public Stack<Integer> transfer(int n){
Stack<Integer> st = new Stack<Integer>();
int division = 0; //余数
while(n>=10){
division = n%10;
st.push(division);
n = n/10;
}
st.push(n); //将最高位压栈
return st;
}
public static void main(String[]args){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String in = "";
try {
in = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int n = 0;
try{
n = Integer.parseInt(in);
} catch(NumberFormatException e){
e.printStackTrace();
}
Transfer tf = new Transfer();
Stack<Integer> s = tf.transfer(n);
/*
while(!s.empty()){
System.out.print(s.pop()); //测试语句
}
*/
HashMap<Integer, String> hp1 = new HashMap<Integer, String>(); //第一个映射表
hp1.put(0, "零"); //根据所在位的数值与中文对应
hp1.put(1, "一");
hp1.put(2, "二");
hp1.put(3, "三");
hp1.put(4, "四");
hp1.put(5, "五");
hp1.put(6, "六");
hp1.put(7, "七");
hp1.put(8, "八");
hp1.put(9, "九");
HashMap<Integer, String> hp2 = new HashMap<Integer, String>(); //第二个映射表
hp2.put(2, "十"); //根据所在位数,与中文对应
hp2.put(3, "百");
hp2.put(4, "千");
hp2.put(5, "万");
hp2.put(6, "十万");
hp2.put(7, "百万");
hp2.put(8, "千万");
hp2.put(9, "亿");
//System.out.println(s.size());
String out = "";
while(!s.isEmpty()){
int temp = s.pop();
if(s.size()==0){
if(temp !=0){
out = out + hp1.get(temp);
}
}
else{
if(temp==0){
out = out + hp1.get(temp);
}
else{
out = out + hp1.get(temp) + hp2.get(s.size()+1);
}
}
}
System.out.println(out);
}
}
对于如2008之类的数,输出的是二千零零八,还需要加以判断,我再去处理下。
还有涉及万以上的数,比如
123456
输出的是一十万二万三千四百五十六,也必须增加判断。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Stack;
/*
* 本程序
*/
public class Transfer {
public Stack<Integer> transfer(int n){
Stack<Integer> st = new Stack<Integer>();
int division = 0; //余数
while(n>=10){
division = n%10;
st.push(division);
n = n/10;
}
st.push(n); //将最高位压栈
return st;
}
public static void main(String[]args){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String in = "";
try {
in = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int n = 0;
try{
n = Integer.parseInt(in);
} catch(NumberFormatException e){
e.printStackTrace();
}
Transfer tf = new Transfer();
Stack<Integer> s = tf.transfer(n);
/*
while(!s.empty()){
System.out.print(s.pop()); //测试语句
}
*/
HashMap<Integer, String> hp1 = new HashMap<Integer, String>(); //第一个映射表
hp1.put(0, "零"); //根据所在位的数值与中文对应
hp1.put(1, "一");
hp1.put(2, "二");
hp1.put(3, "三");
hp1.put(4, "四");
hp1.put(5, "五");
hp1.put(6, "六");
hp1.put(7, "七");
hp1.put(8, "八");
hp1.put(9, "九");
HashMap<Integer, String> hp2 = new HashMap<Integer, String>(); //第二个映射表
hp2.put(2, "十"); //根据所在位数,与中文对应
hp2.put(3, "百");
hp2.put(4, "千");
hp2.put(5, "万");
hp2.put(6, "十万");
hp2.put(7, "百万");
hp2.put(8, "千万");
hp2.put(9, "亿");
//System.out.println(s.size());
String out = "";
while(!s.isEmpty()){
int temp = s.pop();
if(s.size()==0){
if(temp !=0){
out = out + hp1.get(temp);
}
}
else{
if(temp==0){
out = out + hp1.get(temp);
}
else{
out = out + hp1.get(temp) + hp2.get(s.size()+1);
}
}
}
System.out.println(out);
}
}
对于如2008之类的数,输出的是二千零零八,还需要加以判断,我再去处理下。
还有涉及万以上的数,比如
123456
输出的是一十万二万三千四百五十六,也必须增加判断。
展开全部
import java.util.Scanner;
public class test4 {
public void print(String st) {
String r = " ";
int len = st.length();
for (int i = 0; i < len; i++) {
char a = st.charAt(i);
switch (a) {
case '0':
if(i==len-1){}
else if(i==len-5){r+="万";}
else{
if (st.charAt(i + 1) != '0') {r += "零";}
}
break;
case '1':
r += "壹";
break;
case '2':
r += "贰";
break;
case '3':
r += "叁";
break;
case '4':
r += "肆";
break;
case '5':
r += "伍";
break;
case '6':
r += "陆";
break;
case '7':
r += "柒";
break;
case '8':
r += "捌";
break;
case '9':
r += "玖";
break;
}
int w = len - i;
if (a != '0') {
switch (w) {
case 2:
r += "十";
break;
case 3:
r += "百";
break;
case 4:
r += "千";
break;
case 5:
r += "万";
break;
case 6:
r+="十";
break;
case 7:
r+="百";
break;
case 8:
r+="千";
break;
}
}
}
r += "元";
System.out.print(r);
}
public static void main(String[] args) {
test4 t4 = new test4();
System.out.println("请输入数字");
Scanner s = new Scanner(System.in);
String st = s.next();
t4.print(st);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void method2(String num){
String[] arrUnit = {"元","十","百","千","万"}; // 计量单位
String[] arrNum = {"零","一","二","三","四","五","六","七","八","九"}; //大写数字
String[] strNum = num.split("|");
int nNumCount = strNum.length - 2;
// String newStr = "";
for(int i=0; i<strNum.length; i++){
if(strNum[i] != null && !"".equals(strNum[i])){
int n = Integer.parseInt(strNum[i]);
switch(n){
case 0 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 1 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 2 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 3 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 4 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 5 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 6 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 7 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 8 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 9 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
}
}
}
}
public static void main(String args[])
{
new Test2().method2("9999");
}
String[] arrUnit = {"元","十","百","千","万"}; // 计量单位
String[] arrNum = {"零","一","二","三","四","五","六","七","八","九"}; //大写数字
String[] strNum = num.split("|");
int nNumCount = strNum.length - 2;
// String newStr = "";
for(int i=0; i<strNum.length; i++){
if(strNum[i] != null && !"".equals(strNum[i])){
int n = Integer.parseInt(strNum[i]);
switch(n){
case 0 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 1 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 2 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 3 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 4 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 5 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 6 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 7 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 8 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 9 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
}
}
}
}
public static void main(String args[])
{
new Test2().method2("9999");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test {
/**
* 第一个映射表,根据所在位的数值与中文对应汉字
*/
private static HashMap<Integer, String> hp1 = new HashMap<Integer, String>();
/**
* 第二个映射表,根据所在位数,与中文对应单位
*/
private static HashMap<Integer, String> hp2 = new HashMap<Integer, String>();
static {
hp1.put(0, "零");
hp1.put(1, "一");
hp1.put(2, "二");
hp1.put(3, "三");
hp1.put(4, "四");
hp1.put(5, "五");
hp1.put(6, "六");
hp1.put(7, "七");
hp1.put(8, "八");
hp1.put(9, "九");
hp2.put(1, "");
hp2.put(2, "十");
hp2.put(3, "百");
hp2.put(4, "千");
hp2.put(5, "万");
hp2.put(6, "十万");
hp2.put(7, "百万");
hp2.put(8, "千万");
hp2.put(9, "亿");
}
public static void main(String[] args) {
int number = 102034;
Stack<Integer> transfer = transfer(number);
System.out.println(transfer);
int size = transfer.size();
System.out.println(size);
boolean empty = transfer.empty();
System.out.println(empty);
StringBuilder sb = new StringBuilder();
int i = 1;
while (!transfer.empty()) {
Integer pop = transfer.pop();
System.out.println(pop);
//数字
String s1 = hp1.get(pop);
//单位
String s2 = hp2.get(size);
//如果当前数字不是零,向sb里面添加数字和单位
if (pop != 0) {
sb.append(s1).append(s2);
i = 1;
}
//如果当前数字是零,判断是第几个零,如果前面已经有零,不在添加
else {
if (i == 1) {
sb.append(s1);
i++;
}
}
size--;
}
sb.append("元");
String s = sb.toString();
System.out.println(s);
}
public static Stack<Integer> transfer(int n) {
Stack<Integer> st = new Stack<Integer>();
//余数
int division = 0;
while (n >= 10) {
division = n % 10;
st.push(division);
n = n / 10;
}
//将最高位压栈
st.push(n);
return st;
}
}
/**
* 第一个映射表,根据所在位的数值与中文对应汉字
*/
private static HashMap<Integer, String> hp1 = new HashMap<Integer, String>();
/**
* 第二个映射表,根据所在位数,与中文对应单位
*/
private static HashMap<Integer, String> hp2 = new HashMap<Integer, String>();
static {
hp1.put(0, "零");
hp1.put(1, "一");
hp1.put(2, "二");
hp1.put(3, "三");
hp1.put(4, "四");
hp1.put(5, "五");
hp1.put(6, "六");
hp1.put(7, "七");
hp1.put(8, "八");
hp1.put(9, "九");
hp2.put(1, "");
hp2.put(2, "十");
hp2.put(3, "百");
hp2.put(4, "千");
hp2.put(5, "万");
hp2.put(6, "十万");
hp2.put(7, "百万");
hp2.put(8, "千万");
hp2.put(9, "亿");
}
public static void main(String[] args) {
int number = 102034;
Stack<Integer> transfer = transfer(number);
System.out.println(transfer);
int size = transfer.size();
System.out.println(size);
boolean empty = transfer.empty();
System.out.println(empty);
StringBuilder sb = new StringBuilder();
int i = 1;
while (!transfer.empty()) {
Integer pop = transfer.pop();
System.out.println(pop);
//数字
String s1 = hp1.get(pop);
//单位
String s2 = hp2.get(size);
//如果当前数字不是零,向sb里面添加数字和单位
if (pop != 0) {
sb.append(s1).append(s2);
i = 1;
}
//如果当前数字是零,判断是第几个零,如果前面已经有零,不在添加
else {
if (i == 1) {
sb.append(s1);
i++;
}
}
size--;
}
sb.append("元");
String s = sb.toString();
System.out.println(s);
}
public static Stack<Integer> transfer(int n) {
Stack<Integer> st = new Stack<Integer>();
//余数
int division = 0;
while (n >= 10) {
division = n % 10;
st.push(division);
n = n / 10;
}
//将最高位压栈
st.push(n);
return st;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class NumberFormatUtil {
private String toChinese(String string) {
String[] s1 = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
String result = "";
int n = string.length();
for (int i = 0; i < n; i++) {
int num = string.charAt(i) - '0';
if (i != n - 1 && num != 0) {
result += s1[num] + s2[n - 2 - i];
} else {
result += s1[num];
}
}
System.out.println("----------------");
System.out.println(result+"元");
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入字符串:");
String str = scanner.next();
// 将字符串数字转化为汉字
NumberFormatUtil main1 = new NumberFormatUtil();
main1.toChinese(str);
}
}
不能判断小数,最高千亿
private String toChinese(String string) {
String[] s1 = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
String result = "";
int n = string.length();
for (int i = 0; i < n; i++) {
int num = string.charAt(i) - '0';
if (i != n - 1 && num != 0) {
result += s1[num] + s2[n - 2 - i];
} else {
result += s1[num];
}
}
System.out.println("----------------");
System.out.println(result+"元");
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入字符串:");
String str = scanner.next();
// 将字符串数字转化为汉字
NumberFormatUtil main1 = new NumberFormatUtil();
main1.toChinese(str);
}
}
不能判断小数,最高千亿
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询