[英]Changing a variable outside the scope of a method in Java
So I'm trying to change the value of a minimum and maximum guess (with the actual guess being made by a random number generator) in order to stop the program from guessing the same number twice. 因此,我试图更改最小和最大猜测值(实际猜测由随机数生成器进行),以阻止程序两次猜测相同的数字。
I have a method that makes a guess but also tries to set a lowest and highest guess which then changes when the method is used again 我有一个可以猜测的方法,但也尝试设置一个最低和最高的猜测,当再次使用该方法时,它会发生变化
public static int takestab (int low, int high) {
int estimate;
estimate = (low + (int)(Math.random() * ((high - low) + low)));
if (estimate < number) {
lowestguess = estimate;
}
else if (estimate > number) {
highestguess = estimate;
}
return estimate;
}
Also, these are the vars I have outside of the method scope: 另外,这些是我在方法范围之外的变量:
int lowestguess = 1;
int highestguess = 100;
So by running that method, the guess could be 50 and the actual number could be 60. if that's the case, then "lowestguess" becomes 50 so that the function can't guess any lower than 50. 因此,通过运行该方法,猜测可能为50,而实际数字可能为60。如果是这种情况,则“ lowestguess”将变为50,以便函数不能猜测任何低于50的数字。
When I try it this way, the cmd prompt says that it cant find the matching symbol. 当我以这种方式尝试时,cmd提示说它找不到匹配的符号。
Any ideas? 有任何想法吗?
The problem is that lowestguess
is an instance
variable but your trying to access through a static
method. 问题在于,
lowestguess
是一个instance
变量,但是您尝试通过static
方法进行访问。
Options 选件
Passing lowestGuess 通过最低猜测
Example: 例:
public static int takestab(int low, int high, Integer Lowest)
This allows you to also make changes to Lowest as it is by reference . 通过引用 ,这还允许您更改“最低”。
Non Static Method 非静态方法
Change method to public int takestab(int low, int high)
将方法更改为
public int takestab(int low, int high)
Change LowestGuess to static 将LowestGuess更改为静态
You should be careful in multi-threaded environments with this option. 使用此选项时,在多线程环境中应格外小心。
See: 看到:
Finally, if you were programming in an IDE such as eclipse you would have error highlighting in which errors such as this become obvious faster. 最后,如果您在eclipse之类的IDE中进行编程,则会突出显示错误,从而使诸如此类的错误变得更加明显。
lowestguess
is an instance variable, it cannot be accessed without creating an instance. lowestguess
是一个实例变量,如果不创建实例就无法访问它。 If you want to use it inside the static
method then either you create an instance of your class and then use lowestguess
, or if it makes sense then turn lowestguess
as static
. 如果要在
static
方法中使用它,则可以创建类的实例,然后使用lowestguess
,或者如果有意义,则将lowestguess
设为static
。
The reason why non-static
members are not allowed to be used in a static
way is that, the memory initialization of instance variables happen when the object is created. 不允许以
static
方式使用non-static
成员的原因是,创建对象时会发生实例变量的内存初始化。 And a static
method can be called without creating an instance of the class. 并且可以在不创建类实例的情况下调用
static
方法。
A static
method can only use static members (and local var of course). static
方法只能使用静态成员(当然也可以使用局部变量)。 Your members lowestguess
& highestguess
should therefore be declared as static
. 因此,
highestguess
您的成员的lowestguess
和highestguess
声明为static
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.