尝试运行程序时无法访问语句

时间:2016-01-08 04:11:27

标签: java

当我尝试在unreachable statement行编译这段代码时收到错误消息return oocc2.encrypt(input);

  public String decrypt(String input) {
        OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
        OOCaesarCipher oocc2 = new OOCaesarCipher(26-mainKey2);
        return oocc1.encrypt(input);
        return oocc2.encrypt(input);

    }

我认为这是因为我有第二个return语句,但我不知道如何用两个键整数编写这个方法。最初这种方法在另一个类中就是这样的。

public String decrypt(String input) {
        OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
        return oocc1.encrypt(input);
        } 

有人可以分享他/她如何编写更好的方法的知识,以便编译时没有错误  我已经包含了整个代码,以防需要引用。

import edu.duke.*;

public class CaesarCipherTwoKeys {
    private String alphabetLower;
    private String alphabetUpper;
    private String shiftedAlphabetLower1;
    private String shiftedAlphabetUpper1;
    private String shiftedAlphabetLower2;
    private String shiftedAlphabetUpper2;
    private int mainKey1;
    private int mainKey2;
    public CaesarCipherTwoKeys(int key1, int key2) {
        alphabetLower = "abcdefghijklmnopqrstuvwxyz";
        alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        shiftedAlphabetLower1 = alphabetLower.substring(key1) + alphabetLower.substring(0,key1);
        shiftedAlphabetUpper1 = alphabetUpper.substring(key1) + alphabetUpper.substring(0,key1);
        shiftedAlphabetLower2 = alphabetLower.substring(key2) + alphabetLower.substring(0,key2);
        shiftedAlphabetUpper2 = alphabetUpper.substring(key2) + alphabetUpper.substring(0,key2);
        mainKey1 = key1;
        mainKey2 = key2;
    }
    public String encrypt(String input) {
        StringBuilder encryptedInput = new StringBuilder(input);
        OOCaesarCipher oocc1 = new OOCaesarCipher(mainKey1);
        OOCaesarCipher oocc2 = new OOCaesarCipher(mainKey2);

        for (int index=0; index < input.length(); index++) {
            if (index % 2 == 0 || index == 0) {
                encryptedInput.replace(index,index+1,oocc1.encrypt(input.substring(index,index+1)));
            }
            else {
                encryptedInput.replace(index,index+1,oocc2.encrypt(input.substring(index,index+1)));
            }
        }
        return encryptedInput.toString();
    }
    public String decrypt(String input) {
        OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
        OOCaesarCipher oocc2 = new OOCaesarCipher(26-mainKey2);
        return oocc1.encrypt(input);
        return oocc2.encrypt(input);

    }
}

以下是OOCC代码,我将其作为编写此代码的示例:

import edu.duke.*;


public class OOCaesarCipher {
    private String alphabetLower;
    private String alphabetUpper;
    private String shiftedAlphabetLower;
    private String shiftedAlphabetUpper;
    private int mainKey;
    public OOCaesarCipher(int key) {
        alphabetLower = "abcdefghijklmnopqrstuvwxyz";
        alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        shiftedAlphabetLower = alphabetLower.substring(key) + alphabetLower.substring(0,key);
        shiftedAlphabetUpper = alphabetUpper.substring(key) + alphabetUpper.substring(0,key);
        mainKey = key;
    }
    public String encrypt(String input) {
        StringBuilder encrypted = new StringBuilder(input);
        for (int index=0; index < encrypted.length(); index++) {
            Character currChar = encrypted.charAt(index);
            int currentIndex = 0;

            if (Character.isLowerCase(currChar)) {
                currentIndex = alphabetLower.indexOf(currChar);
            }
            else {
                currentIndex = alphabetUpper.indexOf(currChar);
            }

            if (currentIndex != -1 && Character.isLowerCase(currChar)) {
                char newChar = shiftedAlphabetLower.charAt(currentIndex);
                encrypted.setCharAt(index, newChar);
            }
            else if (currentIndex != -1) {
                char newChar = shiftedAlphabetUpper.charAt(currentIndex);
                encrypted.setCharAt(index, newChar);
            }
        }
        return encrypted.toString();
    }
    public String decrypt(String input) {
        OOCaesarCipher oocc = new OOCaesarCipher(26-mainKey);
        return oocc.encrypt(input);
    }
    } 

2 个答案:

答案 0 :(得分:1)

返回一个字符串数组,如下所示:

public String[] decrypt(String input) {

    String[] oocc1And2 = new String[2];
    OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
    OOCaesarCipher oocc2 = new OOCaesarCipher(26-mainKey2);

    oocc1And2[0] = oocc1.encrypt(input);
    oocc1And2[1] = oocc2.encrypt(input);
    return oocc1And2;

 }

答案 1 :(得分:0)

问题在于以下代码

return oocc1.encrypt(input);
return oocc2.encrypt(input);

在java中return statement之后你不能有任何语句,编译器无法理解如何运行。

如果您需要返回两个值,请为其创建Map添加值,然后返回,

或制作POJO,设定值然后返回...

你可能需要修改方法,

以下是将input作为String input。 还可以尝试添加一个输入,对于键,值可以是26-mainKey126-mainKey2

public String decrypt(String input) {
        OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
        return oocc1.encrypt(input);
        }