拼音转数字

题目描述

编写一个函数,输入是一个只包含拼音的字符串 ,请输出对应的数字序列。转换关系如下表:拼音 : yi er san si wu liu qi ba jiu 阿拉伯数字: 1 2 3 4 5 6 7 8 9 输入字符串中只包含小写字母,所有字符都可以正好匹配。

输入

只包含拼音的字符串;

输出

对应的数字序列。

样例输入

yiersansi

样例输出

1234

提示


编写一个main函数实现该功能算法。

地区

南京研究所

public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String input=sc.next().toLowerCase();
		List<Integer> list=new ArrayList<Integer>();
		char[] c=input.toCharArray();
		Stack<Character> stack=new Stack<Character>();
		for(int i=c.length-1;i>=0;i--){
			stack.push(c[i]);
		}
		while(!stack.isEmpty()){
			char ch=stack.pop();
			if(ch=='y'){
				if(!stack.isEmpty()){
					if(stack.peek()=='i'){
						stack.pop();
						list.add(1);
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='e'){
				if(!stack.isEmpty()){
					if(stack.peek()=='r'){
						stack.pop();
						list.add(2);
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='s'){
				if(!stack.isEmpty()){
					if(stack.peek()=='a'){
						stack.pop();
						if(!stack.isEmpty()&&stack.peek()=='n'){
							stack.pop();
							list.add(3);
						}else{
							return ;//输入拼音格式不正确
						}
					}else if(stack.peek()=='i'){
						stack.pop();
						list.add(4);
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='w'){
				if(!stack.isEmpty()){
					if(stack.peek()=='u'){
						stack.pop();
						list.add(5);
						
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='l'){
				if(!stack.isEmpty()){
					if(stack.peek()=='i'){
						stack.pop();
						if(!stack.isEmpty()&&stack.peek()=='u'){
							stack.pop();
							list.add(6);
						}else{
							return ;//输入拼音格式不正确
						}
						
					}else{
						return ;//输入拼音格式不正确
					}
				}else{
					return ;//输入拼音格式不正确
				}
			}
			if(ch=='q'){
				if(!stack.isEmpty()){
					if(stack.peek()=='i'){
						stack.pop();
						list.add(7);
						
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='b'){
				if(!stack.isEmpty()){
					if(stack.peek()=='a'){
						stack.pop();
						list.add(8);
						
					}else{
						return ;//输入拼音格式不正确
					}
				}
			}
			if(ch=='j'){
				if(!stack.isEmpty()){
					if(stack.peek()=='i'){
						stack.pop();
						if(!stack.isEmpty()&&stack.peek()=='u'){
							stack.pop();
							list.add(9);
						}else{
							return ;//输入拼音格式不正确
						}
						
					}else{
						return ;//输入拼音格式不正确
					}
				}else{
					return ;//输入拼音格式不正确
				}
			}
		}
		Object[] obj=list.toArray();
		for(int i=0;i<obj.length;i++){
			System.out.print(obj[i]);
		}
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值