L1-007 念数字 (10 分)java

L1-007 念数字 (10 分)


import java.util.Scanner;
/**
		 * 
		 * @author 曹家伟
		 *
		 */
public class Main {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		if(n>0) {
			String str1=Integer.toString(n);
//		System.out.println(str1);
			int p=str1.length();
			int l=-1;
			char ch[]=new char[p];
//			System.out.println(p);
			ch=str1.toCharArray();
//			System.out.println(ch[1]);
			char a[]= {'0','1','2','3','4','5','6','7','8','9'};
			String str[]= {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
			for(int i=0;i<p;i++) {
				for(int j=0;j<10;j++) {
					if(ch[i]==a[j]) {
						l++;
						if(l<p-1) {
							System.out.print(str[j]+" ");
						}else {
							System.out.print(str[j]);

						}
					}
				}
			}
		}else if(n<0) {
			System.out.print("fu"+" ");
			int n1=Math.abs(n);
			String str1=Integer.toString(n1);
//			System.out.println(str1);
				int p=str1.length();
				int l=-1;
				char ch[]=new char[p];
				ch=str1.toCharArray();
				char a[]= {'0','1','2','3','4','5','6','7','8','9'};
				String str[]= {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
				for(int i=0;i<p;i++) {
					for(int j=0;j<10;j++) {
						if(ch[i]==a[j]) {
							l++;
							if(l<p-1) {
								System.out.print(str[j]+" ");
							}else {
								System.out.print(str[j]);

							}
						}
					}
				}
		}
		else {
			System.out.print("ling");
		}
	}

}

 

很高兴为您解答问题。 "ptal1-007 数字" 是一个题目,要求编写一个程序,将输入的数字转换为对应的中文读音。以下是一个Java实现的示例代码: ```java import java.util.Scanner; public class Main { private static final String[] NUMBER_CN = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] UNIT_CN = {"", "拾", "佰", "仟", "万"}; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int num = scanner.nextInt(); String result = convertToCN(num); System.out.println(result); } scanner.close(); } private static String convertToCN(int num) { if (num == 0) { return NUMBER_CN[0]; } StringBuilder sb = new StringBuilder(); int unitIndex = 0; boolean lastIsZero = true; while (num > 0) { int digit = num % 10; if (digit != 0) { sb.insert(0, UNIT_CN[unitIndex]); sb.insert(0, NUMBER_CN[digit]); lastIsZero = false; } else { if (!lastIsZero) { sb.insert(0, NUMBER_CN[digit]); } lastIsZero = true; } num /= 10; unitIndex += digit == 0 && unitIndex == 4 ? 0 : 1; } return sb.toString(); } } ``` 该程序使用Scanner读取输入的数字,然后调用convertToCN方法将其转换为中文读音。在convertToCN方法中,我们使用了两个数组,别存储数字和单位对应的中文读音。我们采用了一种从低位向高位遍历的方法,每次取出个位数字,根据其值和所处的位数确定其对应的中文读音及单位。最后将所有的中文读音拼接起来即可得到最终结果。 希望这个回答对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值