Java Can';在main中使用的if语句中找不到变量
我试图在我的程序中创建一个菜单,但当我调用一个函数时,终端返回:找不到符号。请注意,在下面的代码中没有出现其他方法。如果没有if语句,这段代码也可以运行Java Can';在main中使用的if语句中找不到变量,java,Java,我试图在我的程序中创建一个菜单,但当我调用一个函数时,终端返回:找不到符号。请注意,在下面的代码中没有出现其他方法。如果没有if语句,这段代码也可以运行 public static void main(String[] args) { int choose = Integer.parseInt (JOptionPane.showInputDialog("Choose an option")); System.out.println("Press
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:错误:找不到符号
字符串解密=解密(加密,密钥)
cipher.java:34:错误:找不到符号
字符串解密=解密(加密,密钥)
cipher.java:41:错误:找不到符号
暴力力量(frcenecrypted) 当您在块内声明变量时,它将成为该块的局部变量,这意味着您不能在该块外使用 在这种情况下,您已在
if
中声明encrypted
和frcencrypted
并在else
中使用它,因此无法找到它
如果有这些行,则必须在if块的外侧声明它
String encrypted = encrypt(str, key);
String frcencrypted = encrypted;
在if块内部。如果将声明移动到If块之前,则可以将它们显示在所需的位置
String encrypted;
String frcencrypted;
if(choose==1)
{
然而,我不清楚你的程序将如何在解密情况下初始化这些变量。据我所知,您只需运行一次程序,然后选择一个选项。当用户选择2时,您打算如何设置加密?在整个执行过程中是否应该有一个循环