So im making a program and a different class refers to an int in a different class:
if (Doihavetools==0 && Stone.StoneCounter>=10 && Wood.WoodCounter>=50){
but in the other class, the int is initialized before the value is "++"ed
int WoodCounter;
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("Chop some wood")) {//ADD A .25 SECOND DELAY INBETWEEN CLICKS
Random Rand = new Random();
int W = Rand.nextInt(3) + 1;
if(W==2){
WoodCounter++;
}
Wood.setText("Click To Collect Wood : Wood:" + WoodCounter);
System.out.println(WoodCounter);
}
}
Is there a way so i can successfully initialize it and add to it at the same time? (i want to be able to continuously add to it)
Thanks in Advance, Jack
You shouldn't declare a woodCounter
variable unless you want to use it for something. Write wood.woodCounter
to access the variable you seem to intend.
I think what you want is for the variable to hold it's value. Try making another variable, a boolean, and before you initialize it, make a check to see if you have already done so. Also, declare this boolean in the class where
if (Doihavetools==0 && Stone.StoneCounter>=10 && Wood.WoodCounter>=50){
is present, at the top. (make it public)
so... then you can
if(initialized == false)
{
int WoodCounter;
initialized = true;
}
Either that, or just use Wood.WoodCounter instead of declaring a new variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.