题目描述 “为了方便记忆,他通过一种算法把这个密码变换成” 错了吧,明明是用算法把明文变换成密码🙄
解题思路:通过多个分支判断语句将字符对应转换,大小写转换不用查ASCII码表,通过字符的加减就能实现。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String cleartext = in.nextLine(); //读取明文
StringBuilder builder = new StringBuilder();
for(int i=0; i < cleartext.length(); i++){
//小写字母转换为数字
if(cleartext.charAt(i)>='a' && cleartext.charAt(i)<='c')
builder.append('2');
else if(cleartext.charAt(i)>='d' && cleartext.charAt(i)<='f')
builder.append('3');
else if(cleartext.charAt(i)>='g' && cleartext.charAt(i)<='i')
builder.append('4');
else if(cleartext.charAt(i)>='j' && cleartext.charAt(i)<='l')
builder.append('5');
else if(cleartext.charAt(i)>='m' && cleartext.charAt(i)<='o')
builder.append('6');
else if(cleartext.charAt(i)>='p' && cleartext.charAt(i)<='s')
builder.append('7');
else if(cleartext.charAt(i)>='t' && cleartext.charAt(i)<='v')
builder.append('8');
else if(cleartext.charAt(i)>='w' && cleartext.charAt(i)<='z')
builder.append('9');
//大写字母'A'~'Y'转换
else if(cleartext.charAt(i)>='A' && cleartext.charAt(i)<='Y')
builder.append((char)(cleartext.charAt(i)+'a'-'A'+1));
//大写字母'Z'单独处理
else if(cleartext.charAt(i) == 'Z')
builder.append('a');
//其他字符不做变换
else
builder.append(cleartext.charAt(i));
}
System.out.println(builder.toString());
}
}
} 
京公网安备 11010502036488号