描述
现在有一种密码变换算法。
九键手机键盘上的数字与字母的对应: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,把密码中出现的小写字母都变成九键键盘对应的数字,如:a 变成 2,x 变成 9.
而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,变成了 y ,例外:Z 往后移是 a 。
数字和其它的符号都不做变换。
数据范围: 输入的字符串长度满足 1 \le n \le 100 \1≤n≤100
输入描述:
输入一组密码,长度不超过100个字符。
输出描述:
输出密码变换后的字符串
![](https://i-blog.csdnimg.cn/blog_migrate/f04d365e3b31bd584efbd58f4180302b.png)
import java.util.*;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
String a = "abc,def,ghi,jkl,mno,pqrs,tuv,wxyz";
Scanner in = new Scanner(System.in);
String temp = in.next();
String ma = "[a-z]";
String mb = "[A-Z]";
String mc = "\\d";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < temp.length(); i++) {
String t = temp.substring(i, i + 1);
if (Pattern.matches(ma, t)) {
int index = a.indexOf(t) + 1;
String s = a.substring(0, index);
int num = s.length() - s.replaceAll(",", "").length();
builder.append(2 + num);
} else if (Pattern.matches(mc, t)) {
builder.append(t);
} else if (Pattern.matches(mb, t)) {
String t1 = t.toLowerCase();
String a1 = a.replaceAll(",", "");
int index = a1.indexOf(t1);
int nIndex = (index + 1) == 26 ? 0 : index + 1;
builder.append(a1.substring(nIndex, nIndex +1));
} else {
}
}
System.out.println(builder.toString());
}
}