【问题标题】:Output doesn't as expected in java code problemjava代码问题中的输出不符合预期
【发布时间】:2020-08-31 14:50:13
【问题描述】:

我是java新手。我正在研究一个问题,以根据随机摆动方向将蜗牛填满牧场。但输出不如预期。

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;
        }
    }
} 

预期输出: 初始门设置: 门 0:1 1号门:1 2号门:1 3号门:-1

实际输出:

初始门设置: 门 0:1 1号门:1 2号门:1 3号门:-1

初始门设置: 门 0:1 门 1:-1 2号门:0 3号门:0

初始门设置: 门 0:-1 1号门:0 2号门:0 3号门:1

我得到 x3 次门随机方向。

【问题讨论】:

  • 检查您在setCorralGates 方法中收到的gate 参数的大小。仅出于调试目的打印它。真的是4吗?不?为什么?
  • 我用 sMAX_GATES 创建了 Gate[] 门对象,大小为 4。
  • ..mFillTheCorral.setCorralGates 在你的 do-while 中调用了多少次?为什么不使用调试器?

标签: java loops methods output new-operator


【解决方案1】:

您的问题是,在您的 Gate 类中,您将字段 mSwing 定义为 static。每个类只存在一个静态字段,这意味着您创建的所有 4 个 Gate 对象将共享相同的 mSwing 值,而不是每个门都有自己的 mSwing 值。

更多信息请阅读:What does the 'static' keyword do in a class?

如果您将字段更改为普通的非静态字段,您将获得您期望的输出:

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;
}

【讨论】:

    【解决方案2】:

    问题是 Gate 的 mSwing 是静态的。当您在 for 循环中调用 gate[i].setSwing(randDir) 时,您每次都在替换相同的静态变量。这就是为什么您的 while 循环仅在 Gate 3 == 1 时结束。

    尝试从mSwing 变量中删除静态。

    【讨论】:

      【解决方案3】:

      我认为问题在于您将变量 mSwing 声明为 static,这意味着它与每个单独的对象无关,但与类有关,所有对象都将共享相同的值。

      因此,当您从对象调用 getSwingDirection() 时,它将返回包含由方法 setSwing() 设置的最后一个值的类静态变量。

      我添加了一个日志来显示结果

      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;
      }
      

      带有 static 修饰符的结果:

      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
      

      没有 static 修饰符的结果:

      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
      

      mSwing 以及从方法 setSwing()getSwingDirection() 中删除 static 修饰符将修复你的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-16
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-04
        相关资源
        最近更新 更多