将接受的字符串转换为字符数组的形式以便于在原位转换;
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = br.readLine()) != null) { char[] cs = s.toCharArray(); for(int i = 0; i < cs.length; i++) { cs[i] = singleTransfer(cs[i]); } System.out.println(new String(cs)); } } public static char singleTransfer(char c) { if ('0' <= c && c <= '9') { return c; } else if ('A' <= c && c <= 'Y') { return (char)(c + 'b' - 'A'); } else if (c == 'Z') { return 'a'; } else { switch (c) { case 'a': case 'b': case 'c': return '2'; case 'd': case 'e': case 'f': return '3'; case 'g': case 'h': case 'i': return '4'; case 'j': case 'k': case 'l': return '5'; case 'm': case 'n': case 'o': return '6'; case 'p': case 'q': case 'r': case 's': return '7'; case 't': case 'u': case 'v': return '8'; default: return '9'; } } } }