在名为 的文件Caesar.java中,实现以下 ( public static) 方法。
一种称为rotate旋转单个字符的方法。它应该有两个参数:第一个是整数 ( int) 移位,第二个是char要旋转的 a,并返回按给定移位旋转的字符,作为 a char。
小写字符应转换为小写字符,大写字符应转换为大写字符,所有其他字符应保持不变。
另一种称为rotate旋转整个字符串的方法。它应该再次接受两个参数:第一个是整数 ( int) 移位,第二个是String要旋转的 a。它应该返回按给定班次旋转的字符串,作为String. 当然你可以在这里使用你的角色旋转方法。
一种main允许对文本进行编码/解码的方法。
偏移量是输入的
输出大概这样
$> java Caesar 3 "The ships hung in the sky in much the same way that bricks don't."
Wkh vklsv kxqj lq wkh vnb lq pxfk wkh vdph zdb wkdw eulfnv grq'w.
$> java Caesar -13 "The ships hung in the sky in much the same way that bricks don't."
Gur fuvcf uhat va gur fxl va zhpu gur fnzr jnl gung oevpxf qba'g.
$> java Caesar 13 The ships hung in the sky in much the same way that bricks don't.
Too many parameters!
Usage: java Caesar n "cipher text"
$> java Caesar 13
Too few parameters!
Usage: java Caesar n "cipher text"
import java.util.Scanner;
public class CaesarCypher {
// Write an encode method
// Write a decode method
public static String Decrypt(String str, int n) {
// TODO Auto-generated method stub
//解密
int k=Integer.parseInt("-"+n);
String string="";
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
{
c+=k%26;//移动key%26位
if(c<'a')
c+=26;//向左超界
if(c>'z')
c-=26;//向右超界
}else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
{
c+=k%26;//移动key%26位
if(c<'A')
c+=26;//向左超界
if(c>'Z')
c-=26;//向右超界
}
string +=c;//将解密后的字符连成字符串
}
return string;
}
public static String Encryption(String str, int k) {
// TODO Auto-generated method stub
//加密
String string="";
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
{
c+=k%26;//移动key%26位
if(c<'a')
c+=26;//向左超界
if(c>'z')
c-=26;//向右超界
}else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
{
c+=k%26;//移动key%26位
if(c<'A')
c+=26;//向左超界
if(c>'Z')
c-=26;//向右超界
}
string +=c;//将解密后的字符连成字符串
}
return string;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String message = "";
int key = 1; //你的密钥
System.out.println("Do you have a message to (1) encode or (2) decode");
int option = in.nextInt();
in.nextLine();
if (option==1){
System.out.println("Type your message to encode");
message = in.nextLine();
String r = Encryption(message, key);
System.out.println(r);
}
if (option==2){
System.out.println("Type your message to decode");
message = in.nextLine();
String r = Decrypt(message, key);
System.out.println(r);
}
}
}
你可以参考下这个
package CaesarCode;
import java.util.ArrayList;
import java.util.Scanner;
public class CeasarCodePlus {
Scanner sc = new Scanner(System.in);
ArrayList list = new ArrayList();
void choose (){
// 初始化密码字符集合,如果你不想手动输入,只能提高时间复杂度喽
char element1 = '0';
for (int i = 0; i < 10; i++) {
list.add(element1);
element1 += 1;
}
for (int i = 'a'; i <= 'z'; i++) {
list.add((char)i);
}
for (int i = 'A'; i <= 'Z'; i++) {
list.add((char)i);
}
select:while (true){
System.out.println("****请选择您的操作****\n1.加密\n2.解密\n3.退出");
int choose = 0;
choose = sc.nextInt();
switch (choose){
case 1: encryption();
break;
case 2: decryption();
break;
case 3:
System.out.println("感谢使用本程序!");
return;
default: System.out.println("请输入正确的选择");
break;
}
}
}
// 加密函数
void encryption(){
String result = "";
System.out.print("请输入您要加密的字符串(仅限字母和数字):");
String str = sc.next();
System.out.print("请输入您要加密密钥(仅限数字):");
//将输入的密钥对集合长度取模,得到key
int key = sc.nextInt() % list.size();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int index = list.indexOf(c);
if(list.size() - list.indexOf(c) <= key){
index = list.indexOf(c) - list.size();
c = (char)list.get(index + key);
}else {
c = (char)list.get(index + key);
}
result += c;
}
System.out.println("加密后的密文为:" + result);
}
// 解密函数
void decryption(){
String result = "";
System.out.print("请输入你要解密的字符串(仅限字母和数字):");
String str = sc.next();
System.out.print("请输入解密的密钥(仅限数字):");
int key1 = sc.nextInt() % list.size();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int index = list.indexOf(c);
if(list.indexOf(c) < key1){
index = list.indexOf(c) + list.size();
c = (char)list.get(index - key1);
}else {
c = (char)list.get(index - key1);
}
result = result + c;
}
System.out.println("解密后的明文为:" + result);
}
public static void main(String[] args) {
CeasarCodePlus code = new CeasarCodePlus();
code.choose();
}
}