这样的程序怎么写?

jsyx 2002-03-05 04:53:55
用户输入为阿拉伯数字,将其转换并输出为汉字表示的数字。

例如,输入为:186573,输出为:一十八万六千五百七十三

哪位给点提示?
...全文
76 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsyx 2002-03-06
  • 打赏
  • 举报
回复
这是我自己写的,9999亿9999万9999以内,实现的比较好。
import java.util.*;
public class ToChinese{
static String[] cnumber={"零","一","二","三","四","五","六","七","八","九"};
static String[] chinanumber={"","十","百","千"};
static String[] chinanumber1={"","万","亿"};
public static void main(String[] args){
int j;
int k;
int m;
int state=0;
List clist=new ArrayList();
List testlist=new ArrayList();
String o="";
if(args.length==0){
System.out.println("please input number");
System.exit(0);
}
char[] test=args[0].toCharArray();
for(int i=0;i<args[0].length();i++){
testlist.add(new Character(test[i]));
if (test[i]<'0' || test[i]>'9'){
System.out.println("invalidate String");
System.exit(0);
}
}
if(Long.parseLong(args[0])==0){
System.out.println("零");
System.exit(0);
}

args[0]=new Long(Long.parseLong(args[0])).toString();

Collections.reverse(testlist);
Iterator it=testlist.iterator();
j=0;
while(it.hasNext()){
test[j]=((Character)it.next()).charValue();
j++;
}

j=args[0].length()/4;
k=args[0].length()%4;
for(int l=0;l<j;l++){
m=l*4;
o="";
state=0;
for(int n=m+3;n>=m;n--){
if(test[n]=='0'){
if(state==0){
o=o+(cnumber[Integer.parseInt((new Character(test[n]).toString()))]);
state=1;
}
} else {
o=o+(cnumber[Integer.parseInt((new Character(test[n]).toString()))]+chinanumber[n-m]);
state=0;
}
}
clist.add(o);
}
it=clist.iterator();
state=0;
if(k>0){
o="";
for(m=j*4+k-1;m>=j*4;m--){
if(test[m]=='0'){
if(state==0){
o=o+(cnumber[Integer.parseInt((new Character(test[m]).toString()))]);
state=1;
}
} else {
o=o+(cnumber[Integer.parseInt((new Character(test[m]).toString()))]+chinanumber[m-j*4]);
state=0;
}
}
clist.add(o);
}

Object[] finish=clist.toArray();
o="";
for(m=0;m<finish.length;m++){
k=((String)finish[m]).length();
if(((String)finish[m]).charAt(k-1)=='零')
finish[m]=((String)finish[m]).substring(0,k-1);
}

for(m=finish.length-1;m>=0;m--)
o=o+finish[m]+chinanumber1[m];
System.out.println(o);
}
}
网络咖啡 2002-03-06
  • 打赏
  • 举报
回复
很简单的啊,还是自己写吧
camry_camry 2002-03-06
  • 打赏
  • 举报
回复
还错了一点,0没输出。
furarmy 2002-03-05
  • 打赏
  • 举报
回复
我觉得只要细心一点,很好解决的。
String value = String.valueOf(19092);
然后从后往前分析,找规律定义就行了
camry_camry 2002-03-05
  • 打赏
  • 举报
回复
错了一点:

if (iszero) {
result = result.deleteCharAt(result.length() -1);
}
if (c != '0')
result.append(num[c - '0']);
--------------------------------------------------------
改为:
--------------------------------------------------------
if (c != '0')
result.append(num[c - '0']);
else {
if (iszero) {
result = result.deleteCharAt(result.length() -1);
}
}
camry_camry 2002-03-05
  • 打赏
  • 举报
回复
觉得很有意思,参与一下。
--------------------------------
static String transform(long n) {
char zero = '零';
char[] num = {
' ','一','二','三','四','五','六','七','八','九'
};
char[] unit = {
'万','十','百','千'
};
StringBuffer result = new StringBuffer();
int site;
boolean iszero = false;
String s = String.valueOf(n);
site = s.length() - 1;
for (int i = 0; i < s.length() - 1; i++) {
char c = s.charAt(i);
if (c != '0') {
iszero = false;
result.append(num[c - '0']);
result.append(unit[site-- % 4]);
}
else {
if (site-- % 4 == 0) {
if (iszero) {
result = result.deleteCharAt(result.length() -1);
}
result.append(unit[0]);
iszero = false;
}
if (iszero) continue;
else {
iszero = true;
result.append(zero);
}
}
}
char c = s.charAt(s.length() - 1);
if (iszero) {
result = result.deleteCharAt(result.length() -1);
}
if (c != '0')
result.append(num[c - '0']);

return result.toString();
} // transform()
zhgzhang 2002-03-05
  • 打赏
  • 举报
回复
懒人哪
g_yxh 2002-03-05
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {
public static void main(String[] args)
{
JFrame frame = new Frame1();
frame.show();
}
}

class Frame1 extends JFrame {
protected TextField textField1=new TextField(50);
Button button=new Button("go");
protected TextField textField2=new TextField(50);
public Frame1(){
setTitle("a test");
setSize(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Container contentPane = getContentPane();
JPanel panel=new JPanel();
button.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button_mouseClicked(e);
}
});
panel.add(textField1);
panel.add(button);
panel.add(textField2);
contentPane.add(panel,"North");
}
public String convert(String in){
String out="";
String inpoint="";
String outpoint="";
String[] digit={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String[] wei={"圆","拾","佰","千","万","拾万","佰万","千万","亿","拾亿","佰亿","千亿","万亿"};
if (in.indexOf(".")>=0){
inpoint=in.substring(in.indexOf(".")+1,in.length());
in=in.substring(0,in.indexOf("."));
}
for (int i=in.length()-1;i>=0;i--) out=digit[Integer.parseInt(in.substring(i,i+1))]+((Integer.parseInt(in.substring(i,i+1))==0)?"":wei[in.length()-i-1])+out;
while (out.indexOf("零零")>=0) out=out.substring(0,out.indexOf("零零"))+out.substring(out.indexOf("零零")+1,out.length());
if (out.substring(out.length()-1).equals("零")) out=out.substring(0,out.length()-1)+"圆";
while (out.substring(0,1).equals("零")) out=out.substring(1,out.length());
if (out.indexOf("万亿")>=0){
String out1=out.substring(0,3);
out=out.substring(3);
while (out.lastIndexOf("万")!=out.indexOf("万")) out=out.substring(0,out.indexOf("万"))+out.substring(out.indexOf("万")+1,out.length());
out=out1+out;
}
else {
while (out.lastIndexOf("万")!=out.indexOf("万")) out=out.substring(0,out.indexOf("万"))+out.substring(out.indexOf("万")+1,out.length());
}
while (out.lastIndexOf("亿")!=out.indexOf("亿")) out=out.substring(0,out.indexOf("亿"))+out.substring(out.indexOf("亿")+1,out.length());
if (out.length()==1) out="零圆";
for (int i=inpoint.length()-1;i>=0;i--) outpoint=digit[Integer.parseInt(inpoint.substring(i,i+1))]+outpoint;
if (outpoint.equals("")==false) out=out.substring(0,out.length()-1)+"点"+outpoint+"圆";
return out;
}

void button_mouseClicked(MouseEvent e) {
String in=this.textField1.getText().trim();
this.textField2.setText(convert(in));
}
}
jsyx 2002-03-05
  • 打赏
  • 举报
回复
谁能给个具体的程序
Luke_cn 2002-03-05
  • 打赏
  • 举报
回复
我想把数字变成字符串,判断长度,确定最大的是十万还是别的什么,再一个循环依次除,例如186573,先除100000,这样可以得到1,即"一十",用switch进行匹配吧
chinajava 2002-03-05
  • 打赏
  • 举报
回复
从后往前
3 三
7 七十
5 五百

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧