简体   繁体   English

如何在 boolean 中使用方法的结果? (在 Java 中)

[英]How do I use the result of a method in a boolean? (In Java)

I'm trying to understand how I can use the result of my method calculateBMI() in a boolean.我试图了解如何在 boolean 中使用我的方法calculateBMI()的结果。

Here's my method with the return value that I need这是我需要的返回值的方法

public int calculateBMI(){
        return (100*100*weight)/(length*length);
    } 

Here's what I've tried so far but I'm a big noob to Java and I know it is wrong, can someone elaborate on how I can use the return value of the calculateBMI() in a boolean for a return value?这是我到目前为止尝试过的,但我是 Java 的大菜鸟,我知道这是错误的,有人可以详细说明如何在 boolean 中使用calculateBMI()的返回值作为返回值吗? Sorry if this is very beginner Java.对不起,如果这是非常初学者 Java。

public boolean hasOverweight(){
    if (calculcateBMI() >= 30) {
        return true;
    } else {
        return false;
    }

EDIT: Solved, thanks guys: Appreciated :)编辑:解决了,谢谢大家:赞赏:)

What you have is perfectly fine.你所拥有的一切都很好。 You get a int and compare it to a known value.您得到一个int并将其与已知值进行比较。 A slightly shorter statement would be稍短的声明将是

    public boolean hasOverweight(){
        return (calculcateBMI() >= 30);
    }

Both are equivalent, and have no real performance difference.两者是等效的,并且没有真正的性能差异。

I´m not sure if i undernstood your question, but you can do this:我不确定我是否理解你的问题,但你可以这样做:

public boolean hasOverweight(){
   return calculcateBMI() >= 30
}

Just with that validation return true o false只需通过该验证即可返回 true o false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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