题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
String s = "";
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
s = in.nextLine();
}
result(s.toCharArray());
}
private static void result(char[] arr){
StringBuffer b = new StringBuffer();
int len = arr.length;
for(int i=0;i<len;i++){
arr[i] = change2Num(arr[i]);
arr[i] = change2Slow(arr[i]);
b.append(arr[i]);
}
System.out.print(b.toString());
}
private static char change2Num(char c){
if(c >= 'a' && c<='c'){
return '2';
} else if(c >= 'd' && c<='f'){
return '3';
} else if(c >= 'g' && c<='i'){
return '4';
} else if(c >= 'j' && c<='l'){
return '5';
} else if(c >= 'm' && c<='o'){
return '6';
} else if(c >= 'p' && c<='s'){
return '7';
} else if(c >= 't' && c<='v'){
return '8';
} else if(c >= 'w' && c<='z'){
return '9';
} else{
return c;
}
}
private static char change2Slow(char c){
if(c >= 'A' && c<= 'Z'){
if(c == 'Z'){
return 'a';
}
int a = 1 + c + 'a' - 'A';
return (char) a;
}
return c;
}
}
雪域灰灰刷题笔记 文章被收录于专栏
雪域灰灰刷题笔记

查看11道真题和解析