【发布时间】:2021-10-06 16:59:35
【问题描述】:
public class pppp {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String s1;
s1=input.nextLine();
char ch[]=new char[s1.length()];
int x;
x=input.nextInt();
int i=0;
int j=0;
int h=0;
for(i=s1.length()-1;i>=0;i--) {
ch[j]=s1.charAt(i);
j++;
}
System.out.println(ch);
for(i=0;i<s1.length();i++) {
if(ch[i]==' ') {
ch[i]='*';
}
else if((ch[i]+x)<122 && ch[i]!='*') {
ch[i]+=x;
}
else if((ch[i]+x-'z')==1) {
ch[i]='a';
}
else if(ch[i]+x-'z'>1) {
ch[i]='a';
ch[i]+=((ch[i]+x-'z')-1);
}
}
System.out.println(ch);
}
}
当我尝试打印“megadeth”并给出键 7 时,它会根据它移动字符来代替 t,这个 '{' 来了。
megadeth
7
htedagem
o{lkhnlt
它现在适用于 t 但如果我写 u 而不是 t 我会得到 N 这是错误的。 这是修改后的代码。
我能做些什么来解决它?
【问题讨论】:
-
你认为应该打印什么?
-
a 而不是 {,因为 t+6 是 z,因为 t=116 在 ascii 值中,所以如果它超过 z,我希望它再次从 a 开始计数。
-
听起来是个好主意。您将如何编写代码?
-
我编辑了代码并写了这个,它适用于 megadeth 的 t 字符,但如果我写 megadeuh 我得到错误的答案。我用 N 代替你。
-
@ThorbjørnRavnAndersen 先生,您现在可以检查一下吗?
标签: java string encryption