简体   繁体   中英

Output doesn't as expected in java code problem

I am new in java. I am working on a problem to fill the corral with snails which is out to pasture based on random swing direction. But output is not as expected.

package fill;

import java.util.Random;

public class FillTheCorral extends Gate {

    public static final int sRANDOM_SEED=1234;

    private static final int sMAX_GATES=4;

    public static void main (String[] args) {
        Random randomNumber=new Random(sRANDOM_SEED);
        FillTheCorral mFillTheCorral=new FillTheCorral();
        Gate[] corral=new Gate[sMAX_GATES];
        for (int i=0; i<corral.length; i++) {
            corral[i]=new Gate();
        }
        do {
            mFillTheCorral.setCorralGates(corral , randomNumber);
        } while (!mFillTheCorral.anyCorralAvailable(corral));
    }
    
    public void setCorralGates(Gate[] gate, Random selectDirection) {
        System.out.println("Initial gate setup:");
        for(int i=0;i<gate.length;i++){
            int randDir = selectDirection.nextInt(3)-1; 
            gate[i].setSwing(randDir);
            System.out.println("Gate " + i + ": "+ randDir);
        }
    }

    public boolean anyCorralAvailable(Gate[] corral) {
        for(int i=0;i<corral.length;i++){
            if(corral[i].getSwingDirection() == IN)
                return true;
        }
        return false ;
    }
}

class Gate {
    public static final int OUT=-1;
    public static final int IN=1;
    public static final int CLOSED=0;
    private static int mSwing;

    public static
    int getSwingDirection () {
        return mSwing;
    }

    public static boolean setSwing (int dir) {
        mSwing=dir;
        if (mSwing == IN) return true;
        if (mSwing == OUT) return true;
        if (mSwing == CLOSED) return true;
        return false;
    }

    public int thru (int count) {
        if (getSwingDirection() == IN) {
            return +count;
        } else if (getSwingDirection() == OUT) {
            return -count;
        } else {
              count*=0;
              return count;
        }
    }
} 

Expected output : Initial gate setup: Gate 0: 1 Gate 1: 1 Gate 2: 1 Gate 3: -1

Actual output:

Initial gate setup: Gate 0: 1 Gate 1: 1 Gate 2: 1 Gate 3: -1

Initial gate setup: Gate 0: 1 Gate 1: -1 Gate 2: 0 Gate 3: 0

Initial gate setup: Gate 0: -1 Gate 1: 0 Gate 2: 0 Gate 3: 1

I am getting x3 times gate random direction.

The issue is that mSwing of Gate is static. When you call gate[i].setSwing(randDir) in the for loop, you are replacing the same static variable every time. That's why your while loop ends only when Gate 3 == 1.

Try to remove static from mSwing variable.

Your problem is that in your Gate class you defined the field mSwing ands its getters/setters as static . A static field only exists once per class, meaning all your 4 created Gate objects will share the same value for mSwing instead of every gate having its own value for mSwing .

For mor information please read: What does the 'static' keyword do in a class?

If you change the field to a normal non-static one you will get the output you expect:

private int mSwing;

public int getSwingDirection () {
    return mSwing;
}

public boolean setSwing (int dir) {
    mSwing=dir;
    if (mSwing == IN) return true;
    if (mSwing == OUT) return true;
    if (mSwing == CLOSED) return true;
    return false;
}

I believe the problem is that you are declaring the variable mSwing as static , which means it's not related to each individual object but with the class, all objects will share the same value.

So, when you call getSwingDirection() from the object, it will return the class static variable which contains the last value set by the method setSwing() .

I added a log to show you the results at

public boolean anyCorralAvailable(Gate[] corral) {
    System.out.println("Value of getSwingDirection():");
    for (int i = 0; i < corral.length; i++) {
        System.out.println("corral[" +i + "]: " + corral[i].getSwingDirection());
        if (corral[i].getSwingDirection() == IN)
            return true;
    }
    return false;
}

Result with the static mofifier:

Initial gate setup:
Gate 0: 1
Gate 1: 1
Gate 2: 1
Gate 3: -1
Value of getSwingDirection():
corral[0]: -1
corral[1]: -1
corral[2]: -1
corral[3]: -1
Initial gate setup:
Gate 0: 1
Gate 1: -1
Gate 2: 0
Gate 3: 0
Value of getSwingDirection():
corral[0]: 0
corral[1]: 0
corral[2]: 0
corral[3]: 0
Initial gate setup:
Gate 0: -1
Gate 1: 0
Gate 2: 0
Gate 3: 1
Value of getSwingDirection():
corral[0]: 1

Process finished with exit code 0

Result without the static mofifier:

Initial gate setup:
Gate 0: 1
Gate 1: 1
Gate 2: 1
Gate 3: -1
Value of getSwingDirection():
corral[0]: 1

Process finished with exit code 0

Removing the static modifier from mSwing and consequently from methods setSwing() and getSwingDirection() would fix your problem.

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.

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