题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.nextLine();
StringBuilder builder = new StringBuilder();
for (char c : input.toCharArray()) {
builder.append(getMapping(c));
}
System.out.println(builder);
}
}
private static char getMapping(char input) {
if ('0' <= input && '9' >= input) {// not change
return input;
}
if ('A' <= input && 'Z' >= input) {
if (input == 'Z') {
return 'a';
}
return (char)('b' - 'A' + input);
}
if ('a' <= input && 'z' >= input) {
return getLowerCaseMapping(input);
}
return input;
}
private static char getLowerCaseMapping(char input) {
if ('a' <= input && 'c' >= input) {
return '2';
}
if ('d' <= input && 'f' >= input) {
return '3';
}
if ('g' <= input && 'i' >= input) {
return '4';
}
if ('j' <= input && 'l' >= input) {
return '5';
}
if ('m' <= input && 'o' >= input) {
return '6';
}
if ('p' <= input && 's' >= input) {
return '7';
}
if ('t' <= input && 'v' >= input) {
return '8';
}
if ('w' <= input && 'z' >= input) {
return '9';
}
return input;
}
}