Java GUI BMI 计算器将无穷大作为 BMI 返回



我浏览了其他一些问题,但没有看到任何关于我的确切问题的内容。 这就是问题所在,我有这个Java GUI BMI计算器,除了我为BMI找回无穷大之外,一切正常。 奇怪的是,当我将输出语句添加到 BMI 时,控制台上显示得很好。 控制台和GUI计算器使用相同的hp.getBMI((,那么为什么一个会得到正确的输出而另一个显示无穷大。 如果我的代码很草率,请原谅我,这是我的第一个 Java 程序,我还没有完全完成编辑它。提前谢谢。

public class HealthProfile 
{
private int age;
private double height, heightFt, heightIn, weight, BMI, maxHR;
private String name, category;
//parameterized constructor
public HealthProfile(String name, int age, double heightFt, double heightIn, double weight)
{
this.name = name;
this.age = age;
this.heightFt = heightFt;
this.heightIn = heightIn;
this.weight = weight;
}
//default constructor
public HealthProfile()
{
this("",0,0,0,0);
}
public double getHeight()
{
height = heightIn + heightFt * 12;  
return height;
}
public double getBMI()
{
BMI = weight * 703 / (height * height);
return BMI;
}
public double getMaxHR()
{
maxHR = 220 - age;
return maxHR;
}
public String getCategory()
{
if (BMI < 18.5)
category = "Underweight";
else if (BMI >= 18.5 && BMI <= 24.9)
category = "Normal";
else if (BMI >= 25 && BMI <= 29.9)
category = "Overweight";
else
category = "Obese";
return category;
}
}

HealthProfile_GUI.java:

public class HealthProfile_GUI extends JFrame implements ActionListener 
{
//Private textfields for name, age, height (feet), height (inches),
//weight, BMI, category, and max heart rate
private JTextField txtName = new JTextField(25);
private JTextField txtAge = new JTextField(3);
...
//Private JLabels for those textfields
private JLabel lblName = new JLabel("Name");
...
private JButton btnCalc = new JButton("Calcualte BMI");
private JButton btnClear = new JButton("Clear");
private HealthProfile hp;
public HealthProfile_GUI()
{
setTitle("Health Profile");
setSize(400,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(9,2,10,10));
c.add(lblName);
c.add(txtName);
...and add all the other text fields and labels
btnCalc.addActionListener(this);
btnClear.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae)
{
boolean notEmpty = true;
if(hp == null)
{
hp = new HealthProfile();
}
if (ae.getSource() == btnCalc) 
{
System.out.println("Calculating BMI");
if(txtName.getText().equals("") || txtName.getText().isEmpty()) 
{
JOptionPane.showMessageDialog(null,"Name is required");
notEmpty = false;
}
if(txtAge.getText().equals("") || txtAge.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Age is required");
notEmpty = false;
}
if(txtHeightFt.getText().equals("") || txtHeightFt.getText().isEmpty()) 
{
JOptionPane.showMessageDialog(null,  "Height (Feet) is required");
notEmpty = false;
}
if(txtHeightIn.getText().equals("") || txtHeightIn.getText().isEmpty()) 
{
JOptionPane.showMessageDialog(null,  "Height (Inches) is required");
notEmpty = false;
}
if(txtWeight.getText().equals("") || txtWeight.getText().isEmpty()) 
{
JOptionPane.showMessageDialog(null,  "Weight is required");
notEmpty = false;
}
if (notEmpty == true) 
{
hp.setName(txtName.getText());
hp.setAge(Integer.parseInt(txtAge.getText()));
hp.setHeightFt(Double.parseDouble(txtHeightFt.getText()));
hp.setHeightIn(Double.parseDouble(txtHeightIn.getText()));
hp.setWeight(Double.parseDouble(txtWeight.getText()));
txtBMI.setText(String.format("%.2f", hp.getBMI()));
txtCat.setText(hp.getCategory());
txtMaxHR.setText(String.format("%.0f", hp.getMaxHR()));
System.out.println(hp.getWeight());
System.out.println(hp.getHeight());
System.out.println(hp.getHeightFt());
System.out.println(hp.getHeightIn());
System.out.println(hp.getBMI());
}
}
else if(ae.getSource() == btnClear)
{
txtName.setText("");
txtAge.setText("");
txtHeightFt.setText("");
txtHeightIn.setText("");
txtWeight.setText("");
txtBMI.setText("");
txtCat.setText("");
txtMaxHR.setText("");
}                       
}                   
}

主类,它只是运行 GUI

public class HealthProfileMain {
public static void main(String[] args) 
{
HealthProfile_GUI hp = new HealthProfile_GUI();
}
}

该方法getBMI()不计算高度:它仅由getHeight()设置,在第一次调用getBMI()之前不调用。

也就是说,事件侦听器正确读取heightFt值和heightIn值,但height值仍为零。更新height也可能是明智的,您阅读的每个时间heightFtheightIn

因此,对getBMI()的第一次调用会产生除以零(因此是无穷大,因为它不是 Java 浮点中的错误(,第二次调用getBMI()得到正确的height值,因此结果正确。

当你第一次调用getBMI((时,高度还没有被设置。但是在第二次调用之前,你调用了getHeight((,它设置了正确的高度值。

最新更新