简体   繁体   English

如何在循环之外使用此变量?

[英]how can i use this variable outside it's loop?

when i try to use the variable "encryptionKey" outside the loop or if-statement it's declared in, it throws a compile error "cannot find symbol".. any idea? 当我尝试在循环或if声明之外使用变量“ encryptionKey”时,会引发编译错误“找不到符号”。

else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{
   char[] encryptionKey = inputPlainResultArray;
   System.out.print("Encryption Key: ");
   System.out.print(encryptionKey);
   System.out.println();
   System.out.println();
   System.out.println();
   System.exit(0);

}
}
}

Because it's local variable , which means you can't access it outside the scope where it's declared. 因为它是局部变量 ,这意味着您不能在声明它的作用域之外访问它。 You should take a look on which types of variables exists in Java 您应该看看Java中存在哪些类型的变量

In your particular case you may use instance variable , so you either declare char[] encryptionKey outside the method: 在您的特定情况下,您可以使用实例变量 ,因此您可以在方法外部声明char[] encryptionKey

 public class YourClass{
   char[] encryptionKey;
   // other methods, fields, etc.
}

and you'll be able to use this variable in any place in this class, or declare inside the method, bit outside the else-if scope: 并且您将可以在此类的任何位置使用此变量,或在方法内部声明, else-ifelse-if范围之外:

char[] encryptionKey = null;
if (...){}

else if (...){
char[] encryptionKey = inputPlainResultArray;
}

so it'll be visible for all entities inside this particular method. 因此它对于此特定方法内的所有实体都是可见的。

That's because the scope of that variable is in the curly braces of the loop / if statement. 这是因为该variable的范围位于loop / if语句的花括号中。 You cannot use it like this. 您不能像这样使用它。 Instead declare it outside and use it. 而是在外部声明并使用它。

In your case it will look something like this: 在您的情况下,它将如下所示:

char[] encryptionKey = null;
if (...)
...
else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{
    encryptionKey = inputPlainResultArray;
    System.out.print("Encryption Key: ");
    System.out.print(encryptionKey);
    System.out.println();
    System.out.println();
    System.out.println();
    System.exit(0);
}

create the variable outside of the method 在方法外创建变量

char[] encryptionKey;

inside of the method then you can have 在方法里面,那么你可以

encryptionKey = ...

the only problem is if you try to call it before you initialize the variable, so be careful, or take precautions such as if(encryptionKey==null) return; 唯一的问题是,如果您在初始化变量之前尝试调用它,请当心,或采取诸如if(encryptionKey==null) return;预防措施if(encryptionKey==null) return;

You cannot access the variable "encryptionKey" from outside the loop because you have declared it inside the loop. 您无法从循环外部访问变量“ encryptionKey”,因为已在循环内部对其进行了声明。 Move the declaration outside and it will work. 将声明移到外面,它将起作用。

char[] encryptionKey;    
else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{

             encryptionKey = inputPlainResultArray;
             ....
}

Use break keyword, not System.exit(0); 使用break关键字,而不是System.exit(0);

if(condition){
        //do something
        break;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM