我目前正在尝试开发一个计算您的BMI,BMR等的简单移动应用程序。
有两个单独的公式来计算男性和女性BMR。
男性:66岁以上(13.7 *体重)+(5 *身高* 100) - (6.8 *年龄)
女性:655+(9.6 *体重)+(1.8 *身高* 100) - (4.7 *年龄)
问题: 我使用了if else语句,如果用户点击了formpage上的“男性”单选按钮,应用程序将识别出用户是男性并使用男性BMR公式来计算他的BMR。男性BMR的计算没有问题,结果显示在calculateinput.html页面上
E.g。如果体重= 100,身高= 2,年龄= 18,性别=男性,我将获得2313.6作为BMR,这是正确的。
但是,如果我点击formpage上的“女性”单选按钮,应用仍然使用男性BMR公式,而不是女性BMR公式来计算她的BMR。 如果体重= 100,身高= 2,年龄= 18,性别=女性,我仍然会得到2313.6,这是不正确的。
似乎我的代码忽略了用户的性别输入并应用男性BMR公式而不管在表单页面上无线电的性别,尽管我的if else声明......
有人可以识别并指出我犯了什么错误吗? 您的解释表示赞赏!
以下是我在common.js的代码
class Program
{
struct element
{
public string name;
public string sex;
public int numComp;
public bool flag;
} ;
static void Main(string[] args)
{
int[] arr = { 5, 1, 6, 3, 4, 5, 8, 3, 9, 2, 6, 7 };
string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
string text = System.IO.File.ReadAllText(appRootDir + "\\File.txt");
string[] words = text.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int counter = 0, pos = 0; int[] storeNum = new int[words.Count() / 3];
element[] storage = new element[words.Count() / 3];
element[] storage2 = new element[words.Count() / 3];
foreach (string str in words)
{
if (counter == 0)
{
storage[pos].flag = false;
storage[pos].name = str;
counter++;
}
else if (counter == 1)
{
storeNum[pos] = storage[pos].numComp = Int32.Parse(str);
counter++;
}
else if (counter == 2)
{
storage[pos].sex = str;
counter = 0; pos++;
}
}
int[] mergeSorted = mergeSort(storeNum); //How to proceed here ?
int posit = 0, counterr = 0;
foreach (int num in mergeSorted)
{
for (int i = 0; i < 4; i++)
{
if (storage[i].numComp == num && storage[i].flag==false)
{
storage[i].flag = true;
storage2[posit++] = storage[i];
break;
}
counterr++;
}
}
Console.ReadKey();
}
private static int[] mergeSort(int[] arr)
{
if (arr.Count() <= 1)
{
return arr;
}
int mid = arr.Count() / 2;
int[] left = new int[mid];
int[] right = new int[arr.Count() - mid];
for (int i = 0; i < mid; i++)
{
left[i] = arr[i];
}
int k = 0;
for (int i = mid; i < arr.Count(); i++)
{
right[k++] = arr[i];
}
int[] leftVal = mergeSort(left);
int[] rightVal = mergeSort(right);
int[] merged = mergesort(leftVal, rightVal);
return merged;
}
private static int[] mergesort(int[] leftVal, int[] rightVal)
{
List<int> finalArr = new List<int>(leftVal.Count() + rightVal.Count());
List<int> left = new List<int>(leftVal);
List<int> right = new List<int>(rightVal);
while (left.Count != 0 && right.Count() != 0)
{
if (left[0] < right[0])
{
finalArr.Add(left[0]);
left.RemoveAt(0);
}
else
{
finalArr.Add(right[0]);
right.RemoveAt(0);
}
}
while (left.Count != 0)
{
finalArr.Add(left[0]);
left.RemoveAt(0);
}
while (right.Count != 0)
{
finalArr.Add(right[0]);
right.RemoveAt(0);
}
return finalArr.ToArray();
}
}
以下是我的显示结果页面上的代码。(名为calculateinput.html)
function bmr(gender, height, weight, age){
var calculatedbmr;
if (gender="male"){
calculatedbmr=66+(13.7 * weight)+(5 * height * 100)-(6.8 * age);
}
else if(gender="female"){
calculatedbmr=655+(9.6 * weight)+(1.8 * height * 100)-(4.7 * age);
}
return calculatedbmr;
}
以下是用户输入其输入的表单页面上的代码。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="js/jquery.mobile-1.4.5.min.css">
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<script src="common.js"></script>
<script type="text/javascript">
var mybmi = localStorage.getItem("bmi");
var weightcategory;
if (mybmi<18.5){
weightcategory = "Underweight";
}
else if ((mybmi>=18.5)&&(mybmi<=24.99)){
weightcategory = "Normal Weight";
}
else if ((mybmi>=25)&&(mybmi<=29.99)){
weightcategory = "Overweight";
}
else if ((mybmi>=30)&&(mybmi<=34.99)){
weightcategory = "Class 1 Obesity";
}
else if ((mybmi>=35)&&(mybmi<=39.99)){
weightcategory = "CLass 2 Obesity";
}
else if (mybmi>=40){
weightcategory = "Morbidly Obese";
}
var theloseorgainmessagethatappears = localStorage.getItem("loseorgain");
var theweightdifference = localStorage.getItem("weightdifference");
var thebmrrate = localStorage.getItem("bmrrate");
</script>
</head>
<body>
<div data-role="page" id="mainpage">
<div data-role="header">
<a href="#" onclick="home()" class="ui-btn ui-icon-home ui-btn-icon-left">Home</a>
<h1>BMI Calculator</h1>
</div>
<div data-role="main" class="ui-content">
<b>Your Results:</b><br><br>
Your weight category: <script>document.write(weightcategory)</script><br><br>
Your BMI is : <script>document.write(mybmi)</script><br><br>
<script>document.write(theloseorgainmessagethatappears)</script><br><br>
Your BMR is : <script>document.write(thebmrrate)</script> <br><br>
Weight Difference: <script>document.write(theweightdifference)</script>
</div>
<div data-role="footer" style="text-align:center;">
Developed by The Dom
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
答案 0 :(得分:5)
您在if
语句中使用了分配而不是相等检查。
替换:
if (gender="male"){
else if(gender="female"){
使用:
if (gender==="male"){
else if(gender==="female"){
答案 1 :(得分:0)
使用'=='进行比较而不是'='。 '='用于将thr rhs变量赋值给lhs变量,'=='用于比较。