本帖最后由 谭培龙 于 2012-8-9 12:06 编辑
- public class Test {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("输入数字:");
- Map<String,String> map = new HashMap<String,String>();
- map.put("0","零");
- 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","玖");
- String number = scan.next();
- for(int i = 0; i < number.length(); i++){
- char c = number.charAt(i);
- System.out.print(map.get(c));
- }
- }
- }
复制代码 我用另外一种方法做也还是不行。。第二种方法
public class Test1{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("输入数字:");
String number = scan.next();
for(int i = 0; i < number.length(); i++){
char c = number.charAt(i);
switch(c){
case 0:
System.out.print("零");
break;
case 1:
System.out.print("壹");
break;
case 2:
System.out.print("贰");
break;
case 3:
System.out.print("叄");
break;
case 4:
System.out.print("肆");
break;
case 5:
System.out.print("伍");
break;
case 6:
System.out.print("陆");
break;
case 7:
System.out.print("柒");
break;
case 8:
System.out.print("捌");
break;
case 9:
System.out.print("玖");
}
}
}
需求是把数字的123456789打印成汉字的壹贰……
第一种方法打印的是null? 第二种方法直接不打印? |
|