题解 | #简单密码#
简单密码
http://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); if (in.hasNextLine()){ String line = in.nextLine(); StringBuffer sb = new StringBuffer(); for(int i=0;i<line.length();i++){ char c = line.charAt(i); if (c >= 'A' && c <= 'Y'){ int v = (int)c; v +=33; char b = (char)v; sb.append(b); continue; } if (c == 'Z'){ sb.append('a'); continue; } if (c >= 'a' && c<='c') { sb.append('2'); continue; } if (c >= 'd' && c<='f') { sb.append('3'); continue; } if (c >= 'g' && c<='i') { sb.append('4'); continue; } if (c >= 'j' && c<='l') { sb.append('5'); continue; } if (c >= 'm' && c<='o') { sb.append('6'); continue; } if (c >= 'p' && c<='s') { sb.append('7'); continue; } if (c >= 't' && c<='v') { sb.append('8'); continue; } if (c >= 'w' && c<='z') { sb.append('9'); continue; } sb.append(c); } System.out.println(sb.toString()); } } }