题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String nextLine = sc.nextLine(); char[] toCharArray = nextLine.toCharArray(); String s = "1--1,abc--2,def--3,ghi--4,jkl--5,mno--6,pqrs--7,tuv--8,wxyz--9,0--0"; String[] strings = s.split(","); ArrayList<HashMap<String,Object>> list = new ArrayList<>(); for (String temp:strings) { String[] split = temp.split("--"); HashMap<String,Object> hashMap = new HashMap<String,Object>(); hashMap.put(split[0],split[1]); list.add(hashMap); } StringBuffer br = new StringBuffer(); for (char c :toCharArray) { if(c >= 'A' && c<= 'Z'){ if(c== 'Z'){ c = 'a'; }else{ c = (++c+"").toLowerCase().charAt(0); } br.append(c); //continue; }else if(c >= 'a' && c<= 'z'){ String temp = ""; for (int i = 0; i < list.size(); i++) { HashMap<String, Object> hashMapTemp = list.get(i); if(hashMapTemp.toString().contains(c+"")){ for (Map.Entry<String, Object> ent:hashMapTemp.entrySet()) { temp = ent.getValue()+""; } } } br.append(temp); }else{ br.append(c); } } System.out.println(br); }