java - Java 类方法中未计算公式

标签 java

Java 11.6 我是 Java 新手,正在尝试创建一个 BMI 计算器,它将输入人的体重、高度并计算 BMI。该程序接收数据,但不显示 BMI 计算的任何答案。由于没有错误,我不确定是我的算法错误还是基本编码错误。

PersonWeight.java 类

import java.time.Year;


public class PersonWeight {


    private double height;
    private double weight;

    public PersonWeight() {

        height = 0;
        weight = 0;

    }

    public PersonWeight(double h, double w) {

        height = h;
        weight = w;

    }



    public void setHeight(double h) {
        this.height = h;
    }

    public double getHeight() {
        return height;
    }

    public void setWeight(double w) {
        this.weight = w;
    }

    public double getWeight() {
        return weight;
    }



    public double ComputeBMI() {

        double bmi = ((weight)/(height*height));
        return bmi;
    }



}

具有 main 方法的测试类

import java.util.Scanner;
public class TestPersonWeight {



    public static void classifyBMI() {
        PersonWeight test1 = new PersonWeight();
        String result="";

        if(test1.ComputeBMI() < 18.5) {
            result = "Underweight ";

        } else if (test1.ComputeBMI() < 25) {
            result = "Normal Weight ";
        }else if (test1.ComputeBMI() < 30) {
            result = "Over Weight ";
        }else  {
            result = "Obese ";
        }
     System.out.printf(result);


    }

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        TestPersonWeight TestPersonWeight = new TestPersonWeight();
        PersonWeight PersonWeight = new PersonWeight()
        System.out.printf("Enter person's Height in Meters: ");
        double h = input.nextDouble();
        PersonWeight.setHeight(h);


        System.out.printf("Enter person's Weight in Kilograms: ");
        double w = input.nextDouble();
        PersonWeight.setWeight(w);

       PersonWeight.ComputeBMI();
        System.out.printf("%n Height: " + PersonWeight.getHeight());
        System.out.printf("%n Weight: " + PersonWeight.getWeight());
        System.out.printf("%n BMI: " , PersonWeight.ComputeBMI());

    }
}

最佳答案

您的程序在最后一个 System.out.printf() 命令中出现错误

System.out.printf("%n BMI: " + PersonWeight.ComputeBMI());
 //Should be plus and not a comma (",")

关于java - Java 类方法中未计算公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60246329/

相关文章:

java - 我需要将 hibernate 与 mongoDB 连接。请提供 mongoDB 的 hibernate.cfg。我有一个,但它不起作用

java - 如果找不到匹配项,替换会做什么? (在引擎盖下)

java - 我如何更快地获得 MongoDB 中的等效函数? WHERE 子句?

java - Java 中的 String.Format (.NET) 等价物?

java - 如何在反射的私有(private)方法上模拟自动连线的私有(private)响应

java:从 URL 获取图像高度和宽度的快速方法

java - java中的最大套接字数

java - 如何列出 gradle 项目的所有依赖项的 JDK 兼容性(如 "sourceCompatibility")?

java - 在 Java 中将 JLabels 数组添加到单个面板?

java - 在 Jframe 中打开新窗口