2010-06-13 10 views
1

其實我正在研究bmi計算器。在哪裏我想要計算以英寸爲單位的高度和以磅爲單位的重量的bmi,並且還需要以釐米爲單位的高度和以千克爲單位的重量的正確公式。我需要Objective-C語法來計算bmi

我已經嘗試過,但無法計算實際值與範圍如下。它超出了範圍。

BMI分類:

* Underweight = <18.5 
* Normal weight = 18.5-24.9 
* Overweight = 25-29.9 
* Obesity = BMI of 30 or greater 
+0

以下英制單位的公式。如果你的結果是關閉的;確保在劃分之前進行「^ 2」計算。 – jensgram 2010-06-13 12:35:03

回答

2

在這裏你去(這裏假設你的原值int s - 您可以輕鬆使用花車,而不會有任何問題):

// Metric 
int heightInCms; 
int weightInKgs; 

// Imperial 
int heightInInches; 
int weightInPounds; 

float height; // metres 
float weight; // kilograms 

// Unit Conversions 
height = (float) heightInCms * 0.01; 
weight = (float) weightInKgs; 

height = (float) heightInInches * 0.0254; 
weight = (float) weightInPounds * 0.4536; 

float BMI = weight/(height * height); 

if (BMI < 18.5) { 
    // Underweight 
} 
else if (BMI < 25.0) { 
    // Normal 
} 
else if (BMI < 30.0) { 
    // Overweight 
} 
else { 
    // Obese 
} 
+0

你好,這個答案被接受?身高將是直立的。 height =(float)heightInCms * 0.01; weight =(float)weightInKgs; height =(float)heightInInches * 0.0254; weight =(float)weightInPounds * 0.4536; float BMI =體重/(身高*身高); – 2016-03-08 07:08:46

+0

@BhaveshLathigara對不起,你能澄清你的意思嗎? – 2016-03-08 23:41:33

+0

// Unit Conversions height =(float)heightInCms * 0.01; weight =(float)weightInKgs; height =(float)heightInInches * 0.0254; weight =(float)weightInPounds * 0.4536; – 2016-05-24 09:47:02

0

我只是猜測你的問題是浮點數和整數。你必須確保你使用CGFloat的重量爲花車,以便它可以包含浮點數

然後你只是做了很多的if else

CGFloat BMI = weight(lb) x 703/height * height 

if (BMI <= 18.5) 
    NSLog(@"Under weight"); 
+1

C中的'^'是一個按位異或,而不是指數運算符。 – 2010-06-13 13:26:18

+0

嗯,這是一個問題,我會盡力解決它 – vodkhang 2010-06-13 14:05:07