题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
StringBuilder result=new StringBuilder();
for(int i=0;i<a.length();i++){
char ch=a.charAt(i);
if(ch>='a'&&ch<='z'){
//小写字母转数字
if(ch=='a'||ch=='b'||ch=='c'){
ch='2';
}else if(ch=='d'||ch=='e'||ch=='f'){
ch='3';
}else if(ch=='g'||ch=='h'||ch=='i'){
ch='4';
}else if(ch=='j'||ch=='k'||ch=='l'){
ch='5';
}else if(ch=='m'||ch=='n'||ch=='o'){
ch='6';
}else if(ch=='p'||ch=='q'||ch=='r'||ch=='s'){
ch='7';
}else if(ch=='t'||ch=='u'||ch=='v'){
ch='8';
}else if(ch=='w'||ch=='x'||ch=='y'||ch=='z'){
ch='9';
}
}else if(ch>='A'&&ch<'Z'){
//大写字母先小写,再后移一位
String temp=""+ch;
temp=temp.toLowerCase();
ch=temp.charAt(0);
ch+=1;
}else if(ch=='Z'){
ch='a';
}
result.append(ch);
}
System.out.println(result);
}
}
}
最暴力的方法,下面尝试用正则做一遍
