英文:
Output doesn't as expected in java code problem
问题
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;
}
}
}
英文:
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.
答案1
得分: 0
问题在于 Gate 的 mSwing
是静态的。当你在 for 循环中调用 gate[i].setSwing(randDir)
时,你每次都在替换同一个静态变量。这就是为什么只有当 Gate 3 == 1 时,你的 while 循环才会结束。
尝试将 mSwing
变量的静态修饰去除。
英文:
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.
答案2
得分: 0
你的问题在于你的 Gate
类中将字段 mSwing
以及它的获取器/设置器定义为了 static
。静态字段在类中只存在一次,这意味着你创建的所有4个 Gate
对象将共享相同的 mSwing
值,而不是每个门都有自己的 mSwing
值。
如果你将该字段更改为普通的非静态字段,你将会得到预期的输出:
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;
}
英文:
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;
}
答案3
得分: 0
我认为问题在于您将变量mSwing声明为static,这意味着它与每个单独的对象无关,而是与类相关联,所有对象将共享相同的值。
因此,当您从对象调用*getSwingDirection()时,它将返回类的静态变量,该变量包含方法setSwing()*设置的最后一个值。
我添加了一个日志来显示您在以下位置的结果:
public boolean anyCorralAvailable(Gate[] corral) {
System.out.println("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;
}
使用static修饰符的结果:
初始门设置:
门 0:1
门 1:1
门 2:1
门 3:-1
getSwingDirection()的值:
corral[0]: -1
corral[1]: -1
corral[2]: -1
corral[3]: -1
初始门设置:
门 0:1
门 1:-1
门 2:0
门 3:0
getSwingDirection()的值:
corral[0]: 0
corral[1]: 0
corral[2]: 0
corral[3]: 0
初始门设置:
门 0:-1
门 1:0
门 2:0
门 3:1
getSwingDirection()的值:
corral[0]: 1
进程以退出代码 0 完成。
没有static修饰符的结果:
初始门设置:
门 0:1
门 1:1
门 2:1
门 3:-1
getSwingDirection()的值:
corral[0]: 1
进程以退出代码 0 完成。
从mSwing变量以及因此从方法*setSwing()和getSwingDirection()*中删除static修饰符将修复您的问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论