给字符串加密,输出密文,其实按照题目所说,只需要针对大小写字母加密,其他不变,那么就分三种情况:
1 输入的字符是大写,则变成小写再往后加1,除Z以外,其他字母都是原来的基础上偏移一位 c+33 ,Z的话直接输出a
2 输入的字符是小写,这里先把所有的小写字母放map里,然后采用流计算,获取到对应的key值,再从map中取到对应的数字
3 其他情况都输出原值
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
char[] ch = str.toCharArray();
HashMap<String,Integer> map = new HashMap<>();
map.put("abc",2);
map.put("def",3);
map.put("ghi",4);
map.put("jkl",5);
map.put("mno",6);
map.put("pqrs",7);
map.put("tuv",8);
map.put("wxyz",9);
for(int i=0;i<ch.length;i++){
if(ch[i]>='a'&& ch[i]<='z'){
final Character cF =ch[i];
//流计算,从map中获得所有的key,成为一个set集合,然后看每个key值
//是否包含输入的这个字符,是的话,就随机打出来获取到这个key,然后再在map获取到值
String key = map.keySet().stream().filter(o->o.contains(cF.toString())).findAny().get();
System.out.print(map.get(key));
}else if(ch[i]>='A'&& ch[i]<='Z'){
if((ch[i]+32)=='z'){
System.out.print('a');
}else{
System.out.print((char)(ch[i]+33));
}
}else{
System.out.print(ch[i]);
}
}
}
}
}


京公网安备 11010502036488号