java语言实现移位密码加密过程

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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值