1、关键点:str2更新字符时,不能使用replace,eg:输入字符YUANzhi1987,第一个字符Y转码后是z,下次遍历到z字符时replace会把Y的转码替换为9,所以最好的方式是StringBuilder,,追加
import java.util.*;
@SuppressWarnings("unchecked")
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String str=sc.next();
System.out.println(test(str));
}
}
public static String test(String str){
char[] array=str.toCharArray();
List<Character> set=new ArrayList<>();
for(char c:array){
set.add(c);
}
StringBuilder str2=new StringBuilder();
for(char c1:set){
String c=String.valueOf(c1);
String num=c;
if("abc".contains(c)){
num="2";
}else if("def".contains(c)){
num="3";
}else if("ghi".contains(c)){
num="4";
}else if("jkl".contains(c)){
num="5";
}else if("mno".contains(c)){
num="6";
}else if("pqrs".contains(c)){
num="7";
}else if("tuv".contains(c)){
num="8";
}else if("wxyz".contains(c)){
num="9";
}else if(Character.isUpperCase(c1)){
if(num.equals("Z")){
num="a";
}else{
num=String.valueOf((char)(c1+1)).toLowerCase();
}
}
str2=str2.append(num);
}
return str2.toString();
}
}
@SuppressWarnings("unchecked")
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String str=sc.next();
System.out.println(test(str));
}
}
public static String test(String str){
char[] array=str.toCharArray();
List<Character> set=new ArrayList<>();
for(char c:array){
set.add(c);
}
StringBuilder str2=new StringBuilder();
for(char c1:set){
String c=String.valueOf(c1);
String num=c;
if("abc".contains(c)){
num="2";
}else if("def".contains(c)){
num="3";
}else if("ghi".contains(c)){
num="4";
}else if("jkl".contains(c)){
num="5";
}else if("mno".contains(c)){
num="6";
}else if("pqrs".contains(c)){
num="7";
}else if("tuv".contains(c)){
num="8";
}else if("wxyz".contains(c)){
num="9";
}else if(Character.isUpperCase(c1)){
if(num.equals("Z")){
num="a";
}else{
num=String.valueOf((char)(c1+1)).toLowerCase();
}
}
str2=str2.append(num);
}
return str2.toString();
}
}