我想编写一个程序,它将接收一个字符串并将2添加到字符串的每个字符,这很简单这里是我的代码。 例子: -在Java中对字符串进行简单加密
String str="ZAP YES";
nstr="BCR AGU" //note Z=B and Y=A
String str=sc.nextLine();
String nstr=""
for(int i=0;i<str.length();i++)
{
char ch=sc.charAt(i);
if(ch!=' ')
{
if(ch=='Z')
ch='B';
else if(ch=='Y')
ch='A';
else
ch=ch+2;
}
nstr=nstr+ch;
}
现在我想的n倍(而不是2),这真是我没能解决,增加的每一个字符。
我可能会想到使用n%26,并使用循环的条件,但我无法解决它如何实现。
想想,如果你有CH为 'Z' 会发生什么?如果你的角色的ascii char值是256,会发生什么? – SMA
你是什么意思,'通过n'? – notyou
与第一种情况相同,n是一个变量,现在它是在运行时决定的。 – quadgen