无法在主

时间:2015-10-19 17:53:21

标签: java

我正在尝试在我的程序中创建一个菜单但是当我调用一个函数时,终端返回:找不到符号。请注意,在下面的代码中不会出现其他方法。也没有if语句,这段代码正在运行。

 public static void main(String[] args)
    {

    int choose = Integer.parseInt (JOptionPane.showInputDialog("Choose an option"));           

    System.out.println("Press 1 to Encrypt");
    System.out.println("Press 2 to Decrypt");
    System.out.println("Press 3 to Bruteforce");


    if(choose==1) 
    { 
        //gets a string to encrypt
        String str = (JOptionPane.showInputDialog("Input Data to encypt:"));

        //gets a key
        int key = Integer.parseInt (JOptionPane.showInputDialog("Input the key:"));


        //prints encryption
        String encrypted = encrypt(str, key);
        String frcencrypted = encrypted;
        System.out.println("Encrypted:" + encrypted);
    }

    else if(choose==2)
    {
        //prints decryption
        String decrypted = decrypt(encrypted, key);
        System.out.println("Decrypted:" + decrypted);
    }

    else if(choose==3)
    {   
        //print bruteforce
        bruteforce(frcencrypted);       
    }

    else
    {
        System.out.println("Wrong value");
    }
}

这里有错误:

cipher.java:34:错误:找不到符号

String decrypted = decrypt(encrypted,key);

cipher.java:34:错误:找不到符号

String decrypted = decrypt(encrypted,key);

cipher.java:41:错误:找不到符号

暴力破解(frcencrypted);

2 个答案:

答案 0 :(得分:0)

当你在一个块中声明一个变量时,它变成该块的局部变量,这意味着你无法在该块之外使用它。

在这种情况下,您已在encrypted块中声明frcencryptedif并在else内使用它,因此无法找到它。

你必须在if块

之外声明它

答案 1 :(得分:0)

你有这些行

    String encrypted = encrypt(str, key);
    String frcencrypted = encrypted;
if块内的

。如果在if块之前移动声明,则可以在需要的位置显示它们。

String encrypted;
String frcencrypted;
if(choose==1) 
{

但是,我不清楚您的程序如何在解密情况下初始化这些变量。据我所知,你运行一次程序并选择一个选项。当用户选择2时,您打算如何设置加密?围绕整个执行是否应该循环?