Java GUI BMI计算器将Infinity作为BMI返回

我浏览了其他一些问题,但没有看到我的确切问题。这是问题所在,我有这个Java GUI BMI计算器,并且一切正常,除了我得到了BMI的无穷大。奇怪的是,当我将输出语句包括到BMI中时,在控制台上的显示就很好。控制台和GUI计算器使用的是相同的hp.getBMI(),因此为什么一个要获得正确的输出而另一个要显示无穷大。如果我的代码草率,请原谅我,这是我的第一个Java程序,我还没有完成编辑。提前致谢。

package business;

/**
 *
 * @author: Christopher Brookens
 */
public class HealthProfile 
{

    /**
     * @param args the command line arguments
     */
    //declare class variables
    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);
    }
    //access modifiers
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return age;
    }
    public void setHeightFt(double heightFt)
    {
    	this.heightFt = heightFt;
    }
    public double getHeightFt()
    {
    	return heightFt;
    }
    public void setHeightIn(double heightIn)
    {
    	this.heightIn = heightIn;
    }
    public double getHeightIn()
    {
    	return heightIn;
    }
    public void setHeight(double height) 
	{
		this.height = height;	
	} 
    public double getHeight()
    {
    	height = heightIn + heightFt * 12;	
    	return height;
    }
    public void setWeight(double weight)
    {
        this.weight = weight;
    }
    public double getWeight()
    {
        return weight;
    }
   
    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;
    }
	
	
    
    
    
}

package presentation;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import business.HealthProfile;

public class HealthProfile_GUI extends JFrame implements ActionListener 
{

		private JTextField txtName = new JTextField(25);
		private JTextField txtAge = new JTextField(3);
		private JTextField txtHeightFt = new JTextField(1);
		private JTextField txtHeightIn = new JTextField(2);
		private JTextField txtWeight = new JTextField(6);
		private JTextField txtBMI = new JTextField(10);
		private JTextField txtCat = new JTextField(10);
		private JTextField txtMaxHR = new JTextField(3);
		private JLabel lblName = new JLabel("Name");
		private JLabel lblAge = new JLabel("Age");
		private JLabel lblHeightIn = new JLabel("Height (Inches)");
		private JLabel lblHeightFt = new JLabel("Height (Feet)");
		private JLabel lblWeight = new JLabel("Weight");
		private JLabel lblBMI = new JLabel("BMI");
		private JLabel lblCat = new JLabel("Category");
		private JLabel lblMaxHR = new JLabel("Max Heart Rate");
		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);
			c.add(lblAge);
			c.add(txtAge);
			c.add(lblHeightFt);
			c.add(txtHeightFt);
			c.add(lblHeightIn);
			c.add(txtHeightIn);
			c.add(lblWeight);
			c.add(txtWeight);
			c.add(btnCalc);
			c.add(btnClear);
			c.add(lblBMI);
			c.add(txtBMI);
			c.add(lblCat);
			c.add(txtCat);
			c.add(lblMaxHR);
			c.add(txtMaxHR);
			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("");
			}						
		}					
}

package presentation;

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

The method getBMI() does not compute the height: it's only set by getHeight(), which is not called before the first call to getBMI().

That is, your event listener correctly reads the heightFt and heightIn values, but height is still zero. It may be wise to update the height as well, each tim you read heightFt and heightIn.

So, the first call to getBMI() produces a division by zero (hence infinity, as it's not an error in Java floating point), and the second call to getBMI() gets the correct value of height, hence a correct result.

点赞
评论
半神经
半神经

首次调用getBMI()时,尚未设置高度。但是在第二次调用之前,您调用了getHeight(),它为height设置了正确的值。

点赞
评论