找不到符号变量key1

时间:2016-01-08 22:04:24

标签: java compiler-errors

我不明白为什么我收到此错误消息# make some fake data set.seed(123) some_list <- lapply(1:16, function(x) { y <- rexp(100) names(y) <- sample.int(1000,100) y }) # identify all possible pairs pairs <- t( combn(length(some_list), 2) ) # note: you could also use: pairs <- expand.grid(1:length(some_list),1:length(some_list)) # but in addition to a-to-b, you'd get b-to-a, a-to-a, and b-to-b # get the intersection of names of a pair of elements with given indices kept for bookkeeping get_intersection <- function(a,b) { list(a = a, b = b, intersection = intersect( names(some_list[[a]]), names(some_list[[b]]) ) ) } # get intersection for each pair intersections <- mapply(get_intersection, a = pairs[,1], b = pairs[,2], SIMPLIFY=FALSE) # print the intersections for(indx in 1:length(intersections)){ writeLines(paste('Intersection of', intersections[[indx]]$a, 'and', intersections[[indx]]$b, 'contains:', paste( sort(intersections[[indx]]$intersection), collapse=', ') ) ) } 。我在私有字符串的顶部定义了key1和key 2,但我仍然遇到此错误。我应该在我的解密方法中包含key1和key2吗? 谢谢。欢迎提出所有建议 编辑:我的道歉,key1和key2未在私人字符串中定义。

cannot find symbol   variable key1

这就是问题所在:

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) {
      CaesarCipherTwoKeys cctk= new CaesarCipherTwoKeys(26 - key1, 26 - key2); 
      String decrypted = cctk.encrypt(input); 
      return decrypted;

     }
    }

1 个答案:

答案 0 :(得分:0)

当你得到&#34时要做的第一件事是找不到符号&#34;错误是检查符号实际声明的位置。运行文本编辑器搜索可以正常工作。

查找具有您在引用它的作用域中指定的名称的变量。除了在参考点之上定义的局部变量之外,这些项目也在范围内:

  • 方法和构造函数参数在整个方法/构造函数的范围内,
  • 实例变量在构造函数和实例方法的范围内
  • 静态变量适用于所有方法和构造函数

现在让我们来看看你的代码。你在构造函数中写这个:

public CaesarCipherTwoKeys(int key1, int key2) {
    ...
    mainKey1 = key1;
    mainKey2 = key2;
}

key1key2是构造函数参数。它们在构造函数的范围内,但是一旦构造函数完成,key1key2都超出了范围;您无法从decrypt访问它们。

mainKey1mainKey2仍然在decrypt范围内,因为它们是实例变量:

CaesarCipherTwoKeys cctk= new CaesarCipherTwoKeys(26 - mainKey1, 26 - mainKey2);