题目:
题目描述
密码变换:1–1, abc–2, def–3, ghi–4, jkl–5, mno–6, pqrs–7, tuv–8 wxyz–9, 0–0,就这么简单,把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X,先变成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。
输入和输出描述
输入:YUANzhi1987 输出:zvbo9441987
思路:这个题目主要是数据结构的设计(怎么设计能够使变化比较方便)
- 代码:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
HashMap<Character,Character> map = new HashMap<>();
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = "abcdefghijklmnopqrstuvwxyz";
String result = "";
while(scan.hasNext()){
String value = scan.next();
for(int i = 0; i < value.length(); i++){
char ch = value.charAt(i);
//如果是大写字母
if(ch >= 'A' && ch <= 'Z'){
int index = upper.indexOf(ch);
result = result + lower.charAt((index+1)%26);
}else if(ch >= 'a' && ch <= 'z'){
if(ch <= 'c'){
result = result + '2';
}else if(ch <= 'f'){
result = result + '3';
}else if(ch <= 'i'){
result = result + '4';
}else if(ch <= 'l'){
result = result + '5';
}else if(ch <= 'o'){
result = result + '6';
}else if(ch <= 's'){
result = result + '7';
}else if(ch <= 'v'){
result = result + '8';
}else if(ch <= 'z'){
result = result + '9';
}
}else{
result = result + ch;
}
}
System.out.println(result);
}
}
}
对于小写字母的设计,我是直接用的if-else if的判断来做的
还有比较机智的设计:
string dict1=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
string dict2=”bcdefghijklmnopqrstuvwxyza22233344455566677778889999”