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; //这里再定义一个返回结果的字符串 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; }}
2020-06-12
最新推荐文章于 2021-12-09 19:44:44 发布