package bag;
import java.util.Scanner;
public class ZhuanHanZi {
//
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入你的数字:");
String[] str=
{"拾","佰","仟","万","拾","佰","仟","亿"};
String a= input.nextLine();
String result =getChinese(a);
int n=Integer.parseInt(result);
if (result.length()<=1)
System.out.println(result+"元整");
else
for(int j=0;j<n;j++){
char m=result.charAt(j);
System.out.print(""+m+""+str[n-j]);
}
input.close();
}
public static String getChinese(String str){
StringBuilder sb= new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
switch(c){
case '0':sb.append("零");break;
case '1':sb.append("壹");break;
case '2':sb.append("贰");break;
case '3':sb.append("叁");break;
case '4':sb.append("肆");break;
case '5':sb.append("伍");break;
case '6':sb.append("陆");break;
case '7':sb.append("柒");break;
case '8':sb.append("捌");break;
case '9':sb.append("玖");break;
default :sb.append(c);
}
}
return sb.toString();
}
}
如果有更好的方法换算钱币的话,感谢提供
新手求指教,这该怎么解啊?
答案:1 悬赏:20 手机版
解决时间 2021-03-14 08:52
- 提问者网友:送舟行
- 2021-03-13 10:29
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-03-13 11:56
import java.util.Scanner;public class ZhuanHanZi {
public static String[] str = {"拾","佰","仟","万","拾","佰","仟","亿"};
public static int position = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入你的数字:");
String a= input.nextLine();
String result =getChinese(a);
print(result);
input.close();
}
public static void print(String s){
int n = s.length();
if (n<=1)
System.out.println(s+"元整");
else{
System.out.print(""+s.substring(0,1)+str[s.length() - 2]);
position++;
String str = s.substring(1);
print(str);
}
}
public static String getChinese(String str){
StringBuilder sb= new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
switch(c){
case '0':sb.append("零");break;
case '1':sb.append("壹");break;
case '2':sb.append("贰");break;
case '3':sb.append("叁");break;
case '4':sb.append("肆");break;
case '5':sb.append("伍");break;
case '6':sb.append("陆");break;
case '7':sb.append("柒");break;
case '8':sb.append("捌");break;
case '9':sb.append("玖");break;
default :sb.append(c);
}
}
return sb.toString();
}
}
只是做了一些简单的打印,还有如超过亿元、中间有多个零时怎么处理。这些都没做,你自己看看吧。
public static String[] str = {"拾","佰","仟","万","拾","佰","仟","亿"};
public static int position = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入你的数字:");
String a= input.nextLine();
String result =getChinese(a);
print(result);
input.close();
}
public static void print(String s){
int n = s.length();
if (n<=1)
System.out.println(s+"元整");
else{
System.out.print(""+s.substring(0,1)+str[s.length() - 2]);
position++;
String str = s.substring(1);
print(str);
}
}
public static String getChinese(String str){
StringBuilder sb= new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
switch(c){
case '0':sb.append("零");break;
case '1':sb.append("壹");break;
case '2':sb.append("贰");break;
case '3':sb.append("叁");break;
case '4':sb.append("肆");break;
case '5':sb.append("伍");break;
case '6':sb.append("陆");break;
case '7':sb.append("柒");break;
case '8':sb.append("捌");break;
case '9':sb.append("玖");break;
default :sb.append(c);
}
}
return sb.toString();
}
}
只是做了一些简单的打印,还有如超过亿元、中间有多个零时怎么处理。这些都没做,你自己看看吧。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯