下面是我用数组实现的,不知道优化了没有,你先看看- import java.util.Scanner;
- public class Text1 {
- public static void main(String[] args) {
- System.out.println("请输入数字");
- Scanner scanner=new Scanner(System.in);
- int num=scanner.nextInt();
- System.out.println("打印:");
- System.out.println(translate(num));
- }
- /*translate(int num){}函数实现将数字转换成相应的汉字*/
- public static String translate(int num){
- String[] char1={"0","1","2","3","4","5","6","7","8","9"};
- String[] char2={"零","一","二","三","四","五","六","七","八","九"};
-
- for(int i=0;i<char1.length;i++){
- if(char1[i].equals(num+"")){
- return char2[i];
- }
-
- }
- return "";
-
- }
- }
复制代码 |