【问题标题】:Setting a limit to an int value为 int 值设置限制
【发布时间】:2011-04-21 19:20:10
【问题描述】:

我想为我在 Java 中拥有的 int 值设置一个限制。我正在创建一个简单的健康系统,我希望我的健康保持在 0 到 100 之间。我该怎么做?

【问题讨论】:

  • 为什么需要设置限制?
  • 我不希望我的健康值低于 0 或高于 100。
  • 我猜你想要比yourInt=Math.max(yourInt, 100); 更复杂一点的东西,所以请你能详细说明一下这个问题吗?
  • 好吧,要么在你得到输入时检查边界,要么创建一个特殊的healtInt 类来为你做这件事。
  • 为什么会超出这些限制?

标签: java math int limit


【解决方案1】:

我建议您创建一个名为 Health 的类,并且您每次检查是否设置了新值是否满足约束:

public class Health {

   private int value;

   public Health(int value) {
      if (value < 0 || value > 100) {
         throw new IllegalArgumentException();
      } else {
         this.value = value;
      }
   }

   public int getHealthValue() {
      return value;
   }


   public void setHealthValue(int newValue) {
    if (newValue < 0 || newValue > 100) {
         throw new IllegalArgumentException();
      } else {
      value = newValue;
    }
   }

}

【讨论】:

  • 谢谢,这正是我想要的方式。 (还不能点击接受按钮,还要再等6分钟。)
  • IllegalArgumentException 是您通常希望用于此类非法参数的内容。模拟您想要在游戏中做的事情的方法(例如在不超出界限的情况下添加或删除 X 健康)也是此类课程的关键。
  • 是的,您也可以使用 IllegalArgumentException 而不是自定义 HelathException。
  • 我会在构造函数中使用 setHeathValue,所以它不必写两次,但除此之外,一切都很好。
  • 我不会那样做,因为你不应该在构造函数中调用可覆盖的方法。
【解决方案2】:

使用 getter/setter 模型。

public class MyClass{
    private int health;

    public int getHealth(){
        return this.health;
    }

    public int setHealth(int health){
        if(health < 0 || health > 100){
            throw new IllegalArgumentException("Health must be between 0 and 100, inclusive");
        }else{
            this.health = health;
        }
    }
}

【讨论】:

    【解决方案3】:

    我会创建一个强制执行此操作的类。

    public class Health {
      private int health = 100;
    
      public int getHealth() {
        return health;
      }
    
      // use this for gaining health
      public void addHealth(int amount) {
        health = Math.min(health + amount, 100);
      }
    
      // use this for taking damage, etc.
      public void removeHealth(int amount) {
        health = Math.max(health - amount, 0);
      }
    
      // use this when you need to set a specific health amount for some reason
      public void setHealth(int health) {
        if (health < 0 || health > 100)
          throw new IllegalArgumentException("Health must be in the range 0-100: " + health);
        this.health = health;
      }
    }
    

    这样,如果你有一个Health 的实例,你就知道它代表了一个有效的健康量。我想您通常只想使用addHealth 之类的方法,而不是直接设置健康状况。

    【讨论】:

      【解决方案4】:

      封装字段并在setter方法中加入check。

      int a;
      
      void setA(int a){
         if value not in range throw new IllegalArgumentException();
      }
      

      【讨论】:

        【解决方案5】:

        在 Java 中没有办法限制原语。您唯一能做的就是为此编写一个包装类。当然,这样做会失去良好的运营商支持,并且必须使用方法(如BigInteger)。

        【讨论】:

          【解决方案6】:
          public void setHealth(int newHealth) {
            if(newHealth >= 0 && newHealth <= 100) {
              _health = newHealth;
            }
          }
          

          【讨论】:

            【解决方案7】:

            创建一个特殊的类没有意义。 要增加 i,请使用:

            public void specialIncrement(int i) { 如果(我

            【讨论】:

            • 这应该如何回答这个问题?
            • 并没有真正强制执行它,因为如果不使用 specialincrementer 就很容易忘记和增加 i。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-11
            • 2014-01-11
            • 2015-04-23
            • 1970-01-01
            • 2020-07-08
            • 1970-01-01
            相关资源
            最近更新 更多