题目描述
编写一个函数,输入是一个只包含拼音的字符串 ,请输出对应的数字序列。转换关系如下表:拼音 : 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]);
}
}