![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
BMI calculator with responses
Hey guys,
I made a BMI calculatorand it works fine. Now I want it to give responses based on the bmi index that you get to an answer. Basically All I need is to figure out the right if then statment combination. Here is my code so far import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public class bmi2 extends JApplet implements ActionListener { JTextField inputLbs, inputInches, displayBmi; public void init() { JLabel labelLbs = new JLabel("Weight (lbs):", SwingConstants.RIGHT); inputLbs = new JTextField(5); JLabel labelInches = new JLabel("Height (inches):", SwingConstants.RIGHT); inputInches = new JTextField(5); JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT); displayBmi = new JTextField(5); displayBmi.setEditable(false); JButton go = new JButton("Compute"); go.addActionListener(this); Container c = getContentPane(); c.setBackground(Color.white); JPanel p = new JPanel(); p.setLayout(new GridLayout(3, 2, 5, 5)); p.add(labelLbs); p.add(inputLbs); p.add(labelInches); p.add(inputInches); p.add(labelBmi); p.add(displayBmi); c.add(p, BorderLayout.CENTER); c.add(go, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { int lbs = Integer.parseInt(inputLbs.getText()); int inches = Integer.parseInt(inputInches.getText()); double bmi = calculateBmi(lbs, inches); DecimalFormat df = new DecimalFormat("00.0"); displayBmi.setText(df.format(bmi)); } private double calculateBmi(int lbs, int inches) { return (double) (lbs/2.2046226) / ((double)(inches*0.0254)*(double)(inches*0.0254)); } } Thanks alot P.S.: for the BMI indexes it should be: less than 18.5 = underweight Normal weight = 18.5-24.9 Overweight = 25-29.9 Obesity = BMI of 30 or greater |
|
|
|
|
|
#2 |
|
Professional Programmer
|
well .. it should be like this :
if(bmi<18.5)System.out.println("Underweight");
else if((bmi>18.5)&&(bmi<24.9))System.out.println("normal");
else if((bmi>25)&&(bmi<29.9))System.out.println("Overweight");
else System.out.println("Obesis");![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
Thats what i was tying. and it compils fine but it simply wont display the message how should I go about this??
Thanks Andrea |
|
|
|
|
|
#4 |
|
Professional Programmer
|
Well, you should have another JTextField, let's say displayIndexBmi
and instead of System.out.println() - you should use displayIndexBmi.setText("Underweight");That's what I meant by saing -- "make some modifications to suit your needs"
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Also, when you don't use >= and <= and instead < and > it's excluding the numbers given to it as a test. Ex. if 25, 29.9, 18.5, etc... any of those numbers were the BMI it would show "obesis".. it should really be this...
if(bmi < 18.5)
displayIndexBmi.setText"Underweight");
else if((bmi >= 18.5) && (bmi <= 24.9))
displayIndexBmi.setText("normal");
else if((bmi >= 25) && (bmi <= 29.9))
displayIndexBmi.setText("Overweight");
else
displayIndexBmi.setText("Obesis"); |
|
|
|
|
|
#6 |
|
Newbie
|
I guess you would use the full duplex operators to create world-class synergy using an ERP pipeline over your BBQ bus.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|