方案一:
import java.util.*;
@SuppressWarnings("unchecned")public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
StringBuffer sb=new StringBuffer();
for(int i=0;i<str.length();i++){
char m=str.charAt(i);
String n=String.valueOf(m);
//此处不能使用replaceALL或者replace,部分场景会有问题,重新定义便利接收
if("abc".contains(n)){
n="2";
}else if("def".contains(n)){
n="3";
}else if("ghi".contains(n)){
n="4";
}else if("jkl".contains(n)){
n="5";
} else if("mno".contains(n)){
n="6";
}else if("pqrs".contains(n)){
n="7";
}else if("tuv".contains(n)){
n="8";
}else if("wxyz".contains(n)){
n="9";
}else if(n.matches("[A-Z]")){
if(n.equals("Z")){
n="a";
}else{
n=String.valueOf((char)(m+1)).toLowerCase();
}
}
sb.append(n);
}
System.out.println(sb);
}
}
方案二:
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();
}
}