我浏览了其他一些问题,但没有发现我的确切问题。这是问题所在,我有这个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();
}
}
答案 0 :(得分:1)
方法getBMI()
不计算高度:它仅由getHeight()
设置,在第一次调用getBMI()
之前不会被调用。
也就是说,您的事件侦听器正确读取了heightFt
和heightIn
的值,但是height
仍然为零。最好也更新height
,即您阅读heightFt
和heightIn
的每个时机。
因此,第一次调用getBMI()
会除以零(因此无穷大,因为这在Java浮点数中不是错误),而第二次调用getBMI()
则得到正确的{ {1}},因此是正确的结果。
答案 1 :(得分:0)
第一次调用getBMI()时,未设置高度。但是在第二个调用之前,您调用了getHeight(),它为height设置了正确的值。