如何使用For循环获取体重指数值? [英] How Do I Use For Loop To Get A Body Mass Index Value?

查看:361
本文介绍了如何使用For循环获取体重指数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,在5千克间隙的for循环中计算体重指数。 BMI公式为:体重(kg)/身高(米平方)= BMI。这怎么样?这是我到目前为止的代码:



Console.Write(用米写高度:);

double t1 = double.Parse(Console.ReadLine());

Console.Clear();



Console.Write(写入起点以kg为单位的重量:);

double t2 = double.Parse(Console.ReadLine());

Console.Clear();



Console.Write(以kg为单位写重量终点:);

double t3 = double.Parse(Console.ReadLine());

Console.Clear();



double i = t1;

double t1s = t1 * t1;

double BMI = t2 / t1s;



for(i = BMI; t2< = t3; i ++)

{

Console.WriteLine(i);

t2 + = 5;

} Console.ReadKey();



我希望结果看起来像这样:

身高1,6米的BMI是:

体重| BMI

60 | 23.44

65 | 25.39

70 | 27.34

...跳至125(结束点)

125 | 48.83

我真的迷失了它是如何工作的,如果有人知道这怎么可行,请回答。

解决方案

也许它会帮助您可视化'for-loop如果您将其视为:

   double  currentWeight = startWeight; currentWeight <  = finalWeight; currentWeight = currentWeight + weightIncrement)
{
// 在此计算BMI
double BMI = currentWeight / heightMetersSquared;

// 将当前结果写入控制台
Console.WriteLine ( BMI:{0} kg。= {1},currentWeight,BMI);
}

这通常可以不太正式写成:

  for  double  cw = startWeight; cw <  = finalWeight; cw + = weightIncrement)
{
// 此处计算BMI
double BMI = cw / heightMetersSquared;

// 将当前结果写入控制台
Console.WriteLine ( BMI:{0} kg。= {1},cw,BMI);
}

在几乎所有编程语言中找到的for循环结构的一个有用的助记符(记忆助手)是:从起始值到最终值的增量为。。


i'm trying to create a program that calculates Body mass index in a for loop with 5kg gaps. BMI formula is: weight(kg)/height(meters squared) = BMI. How would this work? this is the code i got so far:

Console.Write("Write your height in meters: ");
double t1 = double.Parse(Console.ReadLine());
Console.Clear();

Console.Write("Write starting point of weight in kg: ");
double t2 = double.Parse(Console.ReadLine());
Console.Clear();

Console.Write("Write ending point of weight in kg: ");
double t3 = double.Parse(Console.ReadLine());
Console.Clear();

double i = t1;
double t1s = t1 * t1;
double BMI = t2 / t1s;

for (i = BMI; t2 <= t3; i++)
{
Console.WriteLine(i);
t2 += 5;
} Console.ReadKey();

i want the result to look somewhat like this:
BMI for height 1,6m is:
weight | BMI
60 | 23.44
65 | 25.39
70 | 27.34
...skipping to 125(ending point)
125 | 48.83
i'm really lost in how this can work, if anyone knows how this could work please answer.

解决方案

Perhaps it would help you visualize what the 'for-loop does if you think of it as:

for (double currentWeight = startWeight; currentWeight <= finalWeight; currentWeight = currentWeight + weightIncrement)
{
    // calculate BMI here
     double BMI = currentWeight / heightMetersSquared;

    // write current results to Console
    Console.WriteLine("BMI: {0}kg. = {1}", currentWeight, BMI);
}

This could usually be written "less formally" as:

for (double cw = startWeight; cw <= finalWeight; cw += weightIncrement)
{
    // calculate BMI here
     double BMI = cw / heightMetersSquared;

    // write current results to Console
    Console.WriteLine("BMI: {0}kg. = {1}", cw, BMI);
}

A useful mnemonic (memorization helper) for the for-loop construct found in almost every programming language is: "from the start-value to the final-value by increments-of."


这篇关于如何使用For循环获取体重指数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆