java问题,编写程序将一个3位数转成对应的大写汉字,如“301”对应“三百零一”。
2018-08-22
看了这个简单的代码,你应该就懂了
package cn.txet.partice;
import java.util.Scanner;
public class DemoBigNumbers {
int a, b, c;
String bi, si, ge;
public static void main(String[] args) {
DemoBigNumbers db = new DemoBigNumbers();
System.out.println("请输入一个三位数");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();//获取键盘输入的三位数
db.exchange(num);//调用下面的方法
}
public void exchange(int num) {
//使用这个方法,把一个三位数,分解成百位,十位,个位,
if (num / 100 > 0 && num / 100 <=9) {
a = num / 100;
//百位:用switch选择百位字符串
switch (a) {
case 1:
bi = "一百";
break;
case 2:
bi = "二百";
break;
case 3:
bi = "三百";
break;
case 4:
bi = "四百";
break;
case 5:
bi = "五百";
break;
case 6:
bi = "六百";
break;
case 7:
bi = "七百";
break;
case 8:
bi = "八百";
break;
case 9:
bi = "九百";
break;
}
if (num / 10 >= 10 && num / 10 <= 99) {
int s = num / 10;
b = s % (a * 10);
//十位,也是用switch选择十位字符串
switch (b) {
case 0:
si="零";
break;
case 1:
si = "一十";
break;
case 2:
si = "二十";
break;
case 3:
si = "三十";
break;
case 4:
si = "四十";
break;
case 5:
si = "五十";
break;
case 6:
si = "六十";
break;
case 7:
si = "七十";
break;
case 8:
si = "八十";
break;
case 9:
si = "九十";
break;
}
}
if(num%(a*100+b*10)>=0&&num%(a*100+b*10)<=9)
//把个位数从num,三位数字当中分离出来
c=num%(a*100+b*10);
个位:用switch选择个位字符串
switch(c) {
case 0:
ge="零";
break;
case 1:
ge="一";
break;
case 2:
ge="二";
break;
case 3:
ge="三";
break;
case 4:
ge="四";
break;
case 5:
ge="五";
break;
case 6:
ge="六";
break;
case 7:
ge="七";
break;
case 8:
ge="八";
break;
case 9:
ge="九";
break;
}
System.out.println("您输入的数为: "+bi+si+ge);
} else {
System.out.println("请输入三位数");
}
}
}
我按照这个代码打完输出的还是原来的数字
为啥啊
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Hanzi {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个三位数");
Scanner sn=new Scanner(System.in);
int code=sn.nextInt();
Map<Integer,String> map=new HashMap<>();
map.put(1, "壹");
map.put(2, "贰");
map.put(3, "叁");
map.put(4, "肆");
map.put(5, "伍");
map.put(6, "陆");
map.put(7, "柒");
map.put(8, "捌");
map.put(9, "玖");
if(code>100&&code<1000) {
int c=code/100;
int d=code%100;
int e=d/10;
int f=d%10;
if(e==0) {
System.out.println(map.get(c)+"佰零"+map.get(f));
}else {
System.out.println(map.get(c)+"佰"+map.get(e)+"十"+map.get(f));
}
}
}
}