Write a program that calculates and displays a person's body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or under-weight for his or her height. A person's BMI is calculated with the following formula:
BMI = weight x 703 / height^2
Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A sedentary person's weight is considered optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered underweight. If the BMI value is greater than 25, the person is considered overweight.
This is what I have but I keep getting errors when I compile it......
import javax.swing.JOptionPane; // Needed for JOptionPane Class
public class PC3
{
public static void main(String[]args)
{
String input;
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);
// Prompt the user for a height.
input = JOptionPane.showInputDialog("What is your weight ");
weight = Double.parseDouble(input);
// Prompt the user for a weight.
input = JOptionPane.showInputDialog("What is your height ");
height = Double.parseDouble(input);
// Determine whether the user is Optimal, Under, or Overweight.
System.out.println("BodyMassIndex ");
}
}
Having problems with codingBody Mass Index code
Page 1 of 1
5 Replies - 1808 Views - Last Post: 24 February 2010 - 04:57 PM
Replies To: Having problems with coding
#2
Re: Having problems with coding
Posted 23 February 2010 - 09:19 PM
Stop you right here
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);
height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations
Welcome at DIC and please
even for short code snippets
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);
height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations
Welcome at DIC and please
#3
Re: Having problems with coding
Posted 24 February 2010 - 04:34 PM
pbl, on 23 February 2010 - 08:19 PM, said:
Stop you right here
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);
height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations
Welcome at DIC and please
even for short code snippets
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);
height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations
Welcome at DIC and please
Ok Thankyou! Now I got more code and I think I almost got it but it gives me one error on the = sign for the if statement! I can't figure out what i'm doing wrong on that... Here it is :
import javax.swing.JOptionPane; // Needed for JOptionPane Class
public class PC3
{
public static void main(String[]args)
{
String input;
double height;
double weight;
double bodyMassIndex = 100;
// Prompt the user for a weight.
input = JOptionPane.showInputDialog("What is your weight ");
weight = Double.parseDouble(input);
// Prompt the user for a height.
input = JOptionPane.showInputDialog("What is your height ");
height = Double.parseDouble(input);
bodyMassIndex = (weight * 703)/(height * height);
// Determine whether the user is Optimal, Under, or Overweight.
if (BMI > = 18.5 && BMI < 25)
{
JOptionPane.showMessageDialog(null, "You are Optimal weight " + "Your BMI is " + BMI);
}
else if (BMI < 18.5)
{
JOptionPane.showMessageDialog(null, "You are underweight" + "Your BMI is " + BMI);
}
else
{
JOptionPane.showMessageDialog(null, "You are overweight " + "Your BMI is " + BMI);
}
System.out.println("BodyMassIndex ");
}
}
This post has been edited by 385Audio: 24 February 2010 - 04:37 PM
#4
Re: Having problems with coding
Posted 24 February 2010 - 04:41 PM
you have blank space between the ">" operator and the "=" operator, which is illegal.
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
and this code is ok:
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
//wrong, seperated by space if(BMI > = 18.5)
and this code is ok:
//ok, not seperated. if(BMI >= 18.5)
#5
Re: Having problems with coding
Posted 24 February 2010 - 04:42 PM
BMI is not declared, instead, use bodyMassIndex:
bodyMassIndex = (weight * 703)/(height * height);
// Determine whether the user is Optimal, Under, or Overweight.
if (bodyMassIndex >= 18.5 && bodyMassIndex < 25)
This post has been edited by redhotfire0: 24 February 2010 - 04:44 PM
#6
Re: Having problems with coding
Posted 24 February 2010 - 04:57 PM
japanir, on 24 February 2010 - 03:41 PM, said:
you have blank space between the ">" operator and the "=" operator, which is illegal.
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
and this code is ok:
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
//wrong, seperated by space if(BMI > = 18.5)
and this code is ok:
//ok, not seperated. if(BMI >= 18.5)
Thankyou for the info! It worked better like that....
redhotfire0, on 24 February 2010 - 03:42 PM, said:
BMI is not declared, instead, use bodyMassIndex:
bodyMassIndex = (weight * 703)/(height * height);
// Determine whether the user is Optimal, Under, or Overweight.
if (bodyMassIndex >= 18.5 && bodyMassIndex < 25)
Ok I see now what you're saying! I tried it out and it worked for me!! Thankyou....
Page 1 of 1
|
|

New Topic/Question
Reply
MultiQuote







|