为什么我在尝试创建这些方法时遇到此错误?

时间:2014-10-27 17:48:42

标签: c#

我正在尝试初始化3种不同的方法并获得错误。

  

此变量在当前上下文中不存在

我因为范围而在思考它,但我不知道如何在不弄乱它们的情况下更改calculatebmi()calculateextime()方法。

如何摆脱此错误?它抱怨oldbmitimenewbmi

相关代码:

public partial class Program2 : Form
{
    private double _height;
    private double _weight;
    const int caloriesBurned = 10000;
    const int walkingSpeed = 4;
    const double fatCaloriesPerPound = 3500;
    const double metricWalkingSpeed = walkingSpeed / .62137;

    private double calculateBmi(double metricWeight, double metricHeight)
    {
        double oldBmi = metricWeight / Math.Pow(metricHeight, 2);


        double newMetricWeight = metricWeight - (caloriesBurned / (fatCaloriesPerPound * 2.2046));
        double newBmi = newMetricWeight / Math.Pow(metricHeight, 2);

        return oldBmi;
    }

    private double calculateExTime(double metricWeight, double metricHeight)
    {
        double exerciseMultiplier = .0215 * Math.Pow(metricWalkingSpeed, 3)
                                  - .1765 * Math.Pow(metricWalkingSpeed, 2)
                                  + .8710 * metricWalkingSpeed
                                  + 1.4577;
        double time = caloriesBurned / (exerciseMultiplier * metricWeight);

        return time;
    }


        private void displayresults( double _height, double _weight, double oldbmi,double time, double newBmi )
        {
            double newWeight = _weight - (caloriesBurned / fatCaloriesPerPound);
            int feet = (int)_height / 12;
            int inches = (int)_height % 12;
            HeightText.Text = string.Format("{0}ft {1}in", feet, inches);
            Weight.Text = _weight.ToString();
            OriginalBmi.Text = oldBmi.ToString("F2");
            NewBmi.Text = newBmi.ToString("F2");
            NewWeight.Text = newWeight.ToString("F2");
            ExerciseTime.Text = string.Format("{0} hrs {1} min", (int)(time), (int)(time % 60));
        }




 displayresults(_height, _weight,oldBmi,time,newBmi);

2 个答案:

答案 0 :(得分:1)

问题似乎出在你对displayresults方法的调用中。您在那里引用了oldbmi,newbmi和time,但这些对象中没有一个存在,因为它们在本地定义为calculateBmi。这是我的一个假设 - 你还没有真正展示你调用calculateBmi的代码的上下文 - 但似乎是这种情况。

如果您希望在displayresults()中计算的oldBmi和newBmi值可以在程序的其他位置使用,则需要在类级别创建私有字段或公共属性,就像_height和_weight一样。然后你可以将this.newbmi设置为你计算的值,而不是在该范围内创建一个永远不会被使用的新双精度。

所以相反,你班级的顶端会是这样的:

private double _height;
private double _weight;
private double oldbmi = 0;
private double newbmi = 0;
const int caloriesBurned = 10000;
const int walkingSpeed = 4;
const double fatCaloriesPerPound = 3500;
const double metricWalkingSpeed = walkingSpeed / .62137;

你的calculateBmi方法看起来像这样:

private double calculateBmi(double metricWeight, double metricHeight)
{
    this.oldBmi = metricWeight / Math.Pow(metricHeight, 2);


    double newMetricWeight = metricWeight - (caloriesBurned / (fatCaloriesPerPound * 2.2046));
    this.newBmi = newMetricWeight / Math.Pow(metricHeight, 2);

    return oldBmi;
}

你可以用this.oldbmi和this.newbmi调用displayresults。

您应该知道代码中几乎所有内容都违反了.NET命名约定。变量不应该以' _'开头,consts应该是CamelCase,私有字段应该是pascalCase,方法应该始终是CamelCase。请仔细阅读:http://msdn.microsoft.com/en-us/library/vstudio/618ayhy6%28v=vs.100%29.aspx。这里的许多人都遵循这些惯例,这将使您在将来更容易获得帮助。

答案 1 :(得分:0)

您正在调用方法displayresults,该方法需要一些参数。

每当你调用一个方法时,你需要将值a / c传递给方法参数的DataType。没有必要像方法那样传递具有确切名称的变量(我认为你对此感到困惑)。

所以正确的语法就像:

displayresults(10, 20, 30, 40, 50);

如果要传递变量,则应在当前上下文中使用它们:

double _height = 10;
double _weight = 20;
double oldBmi = 30;
double time = 40;
double newBmi = 50;

displayresults(_height, _weight, oldBmi, time, newBmi);