我是java的新手,我正在尝试为我的控制台应用程序创建一个GUI。我编写的代码只需要帮助修复这些错误。该程序似乎运行正常,直到我输入信息并且BMI读取无限并且它会查看所有数据。如果有人能够帮我清理我的代码以使其正常工作它会帮助我很多因为我是对此有麻烦。
以下是每个班级的代码:
健康档案类
public class HealthProfile
{
// attributes
private String name;
private short age;
private float weight;
private float height; //measured in total inches
//constructors
public HealthProfile()
{
name = "unknown";
age = 0;
weight = 0.0f;
height = 0.0f;
}
public HealthProfile(String name, short age, float weight, float height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
//behaviors
public float getBMI()
{
return (float) ((weight *703) / Math.pow(height, 2.0f));
}
public String getCategory()
{
float bmi = getBMI();
if(bmi < 18.5f )
return "Underweight";
else if( bmi >= 18.5f && bmi < 25)
return "Normal";
else if (bmi >= 25 && bmi < 30)
return "Overweight";
else
return "Obese";
}
public float getMaxHR()
{
return 220 - age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(int i) {
if ( i > 0 && i < 150)
this.age = (short) i;
else
this.age = 0;
}
public float getWeight() {
return weight;
}
public void setWeight(double d) {
if (d > 0.0f && d <1000.0f)
this.weight = (float) d;
else
this.weight = 0.0f;
}
public float getHeight() {
return height;
}
public void setHeight(float height_feet, float height_inches) {
this.height = (height_feet *12) + height_inches;
}
}
GUI类:
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;
public class HealthProfileGUI extends JFrame
{
//Create GUI components
private JTextField txtName, txtAge, txtWeight, txtHeightFt, txtHeightIn,
txtBMI, txtCategory, txtMxHeartRate;
private JButton btnDisplay, btnClear;
private HealthProfile hthPro;
//Constructor for building the GUI
public HealthProfileGUI()
{
//Add a name to the GUI
super("Health Profile");
hthPro = new HealthProfile();
//instantiate components
txtName = new JTextField (20);
txtAge = new JTextField(10);
txtWeight = new JTextField(10);
txtHeightFt = new JTextField(10);
txtHeightIn = new JTextField(10);
btnDisplay = new JButton ("Display");
btnClear = new JButton ("Clear");
txtBMI = new JTextField(10);
txtCategory = new JTextField(10);
txtMxHeartRate = new JTextField(10);
//set layout
setLayout(new GridLayout(0,2)) ;
//Add components to frame
add(new JLabel("Name"));
add(txtName);
add(new JLabel("Age"));
add(txtAge);
add(new JLabel("Weight"));
add(txtWeight);
add(new JLabel("Height-Feet"));
add(txtHeightFt);
add(new JLabel("Height-Inches"));
add(txtHeightIn);
add(btnDisplay);
add(btnClear);
add(new JLabel("BMI"));
add(txtBMI);
add(new JLabel ("Category"));
add(txtCategory);
add(new JLabel ("Max Heart Rate"));
add(txtMxHeartRate);
//set up event handlers
//create an object
ButtonHandler handler = new ButtonHandler();
//connect the buttons to the ActionListener using this object
btnDisplay.addActionListener(handler);
btnClear.addActionListener(handler);
}//end constructor
private class ButtonHandler implements ActionListener
{ //Start class
public void actionPerformed(ActionEvent e)
{
//code to process the button events goes here
if (e.getSource() == btnDisplay)
{
//VERIFY that user has provided all input
if (txtName.getText().isEmpty() ||
txtAge.getText().isEmpty() ||
txtWeight.getText().isEmpty() ||
txtHeightFt.getText().isEmpty()||
txtHeightIn.getText().isEmpty()
)//missing input
{
JOptionPane.showMessageDialog(null, "Please provide all input");
return; //exit this method so user can correct
}
{
hthPro.setName(txtName.getText());
try
{
hthPro.setAge(Integer.parseInt(txtAge.getText()));
hthPro.setWeight(Double.parseDouble(txtWeight.getText()));
//hthPro.setHeight(Double.parseDouble(txtHeightFt.getText()));
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Hours, rate must be numeric");
}
txtBMI.setText(String.valueOf(hthPro.getBMI()));
txtCategory.setText(String.valueOf(hthPro.getCategory()));
txtMxHeartRate.setText(String.valueOf(hthPro.getMaxHR()));
}
}
}
}
}
主:
import javax.swing.JFrame;
public class Lab2Main {
public static void main(String[] args)
{
HealthProfileGUI frame = new HealthProfileGUI();
frame.setSize(400, 300); //these numbers are in pixels
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
再次感谢任何可以帮助我的人,我希望问题不是那么大。
答案 0 :(得分:0)
BMI是无限的,因为Math.pow(0, 2.0f)
分割的结果会取消注释setHeight
行:
hthPro.setHeight(Float.parseFloat(txtHeightFt.getText()),
Float.parseFloat(txtHeightIn.getText()));
然后让我们看看你的下一个问题。