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/