【问题标题】:Boolean if statement布尔 if 语句
【发布时间】:2013-03-26 00:03:29
【问题描述】:
public class Health
{
    boolean dependency;
    String insuranceOwner = "";
    static final int basicHealthFee = 250;
    static final int healthDiscount = 20;

    public Health(boolean dependent, String insurance)  
    {  
        dependency = dependent;  
        insuranceOwner = insurance;  
    }

    public double computeCost()
    {
        double healthFee;
        if (dependency == true)
        {
            healthFee = basicHealthFee - (basicHealthFee * (healthDiscount/100.0));
        }
        else 
        {
            healthFee = basicHealthFee;
        }

        return healthFee;
    }
}

 Health h34 = new Health(true, "Jim");         
 System.out.println("discount price: " + h34.computeCost());

当我输入 true 作为构造函数的参数时,我的 computeCost 方法仍然运行该块,就好像依赖关系 == false 一样。有什么原因吗?

【问题讨论】:

  • 有一个未知数 - 从对象实例化到调用此方法之间会发生什么?就目前而言,如果dependency,那么第一部分必须被执行。
  • 你试过调试你的程序吗?
  • 您是否正在使用为调用此方法而创建的同一个实例?是依赖static吗?很多可能性。在您创建Health 实例并调用方法computeCost() 的位置发布代码。
  • @RyanSisson:还有更多。发布您的main() 方法。仅该代码并不能告诉我们适当的事件链。不要让锁定的问题吓到你;这将为您提供适当的时间以使问题处于可回答状态。
  • 我不认为它每次都在运行虚假案例。你的问题是整数截断。 20/100 == 0 的整数数学运算,因此 basicHealthFee 总是乘以零。

标签: java bluej


【解决方案1】:

您正在成为整数除法的牺牲品。 20/100 == 0,乘以 0。要解决这个问题,请将您的 static final int 声明更改为双精度数。

static final double basicHealthFee = 250D;
static final double healthDiscount = 20D;

D 定义了一个double literal

【讨论】:

    【解决方案2】:

    您需要将 basicHealthFee 和 healthDiscount 定义为double。由于您已将它们定义为整数,因此您有以下等式:healthFee = basicHealthFee - (basicHealthFee * (healthDiscount/100)); 变为 basicHealthFee - ( basicHealthFee * (20/100)) 变为 basicHealthFee - (basicHealthFee * 0) -> basicHealthFee - 0

    从构造函数中获取值的 if 语句是正确的。

    【讨论】:

    • 谢谢par,这就是问题所在。还要感谢所有帮助尝试解决问题的人
    • 没问题。由于您是新手,请在收到有帮助的回复时将其中一个答案标记为正确。 Makoto 先发帖,所以把它给他(或她酌情;-)。
    【解决方案3】:

    您的问题与布尔值无关。这是由于整数的除法。请按以下方式更改程序。 静态最终双健康折扣 = 20d; static final double basicHealthFee = 250d;

    package com.stackoverflow.test;
    
    public class Health {
        boolean dependency;
        String insuranceOwner = "";
        static final double basicHealthFee = 250d;
        static final double healthDiscount = 20d;
    
        public Health(boolean dependent, String insurance) {
            dependency = dependent;
            insuranceOwner = insurance;
        }
    
        public double computeCost() {
            double healthFee;
            if (dependency == true) {
                healthFee = basicHealthFee
                        - (basicHealthFee * (healthDiscount / 100.0d));
            } else {
                healthFee = basicHealthFee;
            }
    
            return healthFee;
        }
    
        public static void main(String args[]) {
            Health h34 = new Health(true, "Jim");
            System.out.println("discount price: " + h34.computeCost());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-01
      • 2015-12-21
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 2016-11-08
      • 2018-04-17
      相关资源
      最近更新 更多