-2
我是非常新的编程和无法找出问题与我的Cesar密码我的加密方法工作正常,但我的解密方法不起作用。Ceasar密码方法的解密不工作,并没有打印
主要类
package com.company;
import java.util.Scanner;
public class Main {
public ceasarCipher ceasarCipher;
public static void main(String[] args) {
com.company.ceasarCipher utilityClass = new ceasarCipher();
int inShift;
String inString;
String encryptedString = "";
Scanner sca = new Scanner(System.in);
System.out.println("Please enter a string to encrypt");
inString = sca.nextLine();
System.out.println("=======================================================================================");
System.out.println("In this program");
System.out.println("Press 1 to encrypt your string by shifting it forward a certain amount");
System.out.println("Press 2 to decipher your string by shifting it backwards a certain amount");
System.out.println("Press 3 to give you all possible combinations of the shift in order to decipher");
System.out.println("Press 4 to exit the program");
System.out.println("=======================================================================================");
while(!sca.hasNext("x") && !sca.hasNext("X")){
int userInput;
userInput = sca.nextInt();
switch (userInput){
case 1:
System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted forwards in order to encrypt it");
inShift = sca.nextInt();
System.out.println(utilityClass.ccEncrypt(inShift,inString));
case 2:
System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted backwards in order to decipher it");
inShift = sca.nextInt();
System.out.println(utilityClass.ccDecipher(inShift,encryptedString));
}
}
}
}
子类与方法
package com.company;
/**
* Created by Jake on 14/04/2017.
*/
public class ceasarCipher {
private int maxVal = 26;
//private int inShift;
//private String inString;
private String letters = "abcdefghijklmnopqrstuvwxyz";
//-----------------------------------------------------------
public String ccEncrypt (int inShift, String inString) {
String encryptedString = "";
char modulo;
int pos;
inString = inString.toLowerCase();
for (int i = 0; i < inString.length(); i++) {
pos = letters.indexOf(inString.charAt(i));
modulo = this.letters.charAt((pos + inShift) % maxVal);
encryptedString += modulo;
}
return encryptedString;
}
//----------------------------------------------------------------------
public String ccDecipher (int inShift, String encryptedString) {
String decipheredString = "";
char modulo;
int pos;
for (int i = encryptedString.length() - 1; i >= 0; i--) {
pos = letters.indexOf(encryptedString.charAt(i));
modulo = this.letters.charAt((pos - inShift) % maxVal);
decipheredString += modulo;
}
return decipheredString;
}
// -----------------------------------------------------------------------
public void ccDecipherAll (String inString){
}
}
你是什么意思*“无需打印”*?至少是否打印“请输入要加密的字符串”?如果是这样,请[编辑]您的问题以显示实际输出以及您的输入。 –
是的,它确实是更具体,它不是打印我的解密方法,在案例2 – Jake77
因此,你输入密文,然后程序结束?这看起来像一个ArrayIndexOutOfBoundsException情况,weston的答案是解决这个问题的恰当方法。你在任何地方看到这个例外吗?仔细观察。 –