import java.util.Scanner;
public class Cipher
{
public static void main(String [] args)
{
Scanner kb=new Scanner(System.in);
System.out.println("Please enter a String: ");
String str=kb.nextLine();
System.out.println("Please enter the offset value: ");
int offset=kb.nextInt();
Cipher c = new Cipher(); //new一个类Cipher的实例
System.out.println(c.encrypt(str, offset)); //执行encrypt()方法,并输出
}
public String encrypt(String str,int offset)
{
String result = str; //这里定义一个返回结果的字符串.String.replace()方法只是返回改过后的字符串,并不修改原字符串。
for(int i=0;i<str.length();i++)
{
int a=(int)str.charAt(i)+offset;
if(a>126)
{
result = str.replace(str.charAt(i),(char)(a-95));
}
else
result = str.replace(str.charAt(i),(char)(a));
}//return str; //永远都是返回原来的字符串
return result;
}
}
java语言实现移位密码加密过程
最新推荐文章于 2022-01-14 21:52:15 发布