如何从一个条件访问信息并稍后在不同的条件中使用它?

How can I access information from one conditional and use it later in a different conditional?

我正在尝试从方程式 (BMR) 中获取答案

if(gender == "F")  
{   
    BMR = 655+(4.35 * weight) + (4.7 * height) - (4.7 * userAge );
}  
else if(gender == "M")  
{   
    BMR = 66+(6.23 * weight) + (12.7 * height) - (6.8 * userAge); 
}   
Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender); 
Console.WriteLine ("Your BMR is " + BMR); 

并在这里使用它

static void ProcessChoice (int c)
{   
    double allowedCalories;
    if (c == 1) {
        allowedCalories = BMR * 1.2;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 2) {
        allowedCalories = BMR * 1.375;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 3) {
        allowedCalories = BMR * 1.55;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 4) {
        allowedCalories = BMR * 1.725;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 5) {
        allowedCalories = BMR * 1.9;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
}

但我总是出错。

完整代码如下:

using System;

namespace Manning_C__10_23_17_Lab_Five
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string name;  
            double height, weight;   
            int userAge;  
            string gender;  
            double BMR = 0; 

            Console.Write("Enter your name: ");  
            name = Console.ReadLine ();  
            Console.Write("Enter your height in inches: ");  
            height = Convert.ToDouble(Console.ReadLine ());  
            Console.Write ("Enter your weight in pounds: ");  
            weight = Convert.ToDouble(Console.ReadLine ());  
            Console.Write ("Enter your age: ");  
            userAge = Convert.ToInt32(Console.ReadLine ());  
            Console.Write ("Enter your gender as M or F ");  
            gender = Console.ReadLine ();  
            gender = gender.ToUpper(); 

            if(gender == "F")  
            {   
                BMR = 655+(4.35 * weight) + (4.7 * height) - (4.7 * userAge );
            }  
            else if(gender == "M")  
            {   
                BMR = 66+(6.23 * weight) + (12.7 * height) - (6.8 * userAge); 
            }   
            Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender); 
            Console.WriteLine ("Your BMR is " + BMR); 

            int choice;
            do {
                PrintMenu ();
                choice = Int32.Parse (Console.ReadLine ());
                ProcessChoice (choice);
            } while (choice !=6);
            Console.WriteLine ("Thanks for using this system");
        }

        public static void PrintMenu()
        {
            Console.WriteLine("Main Menu");
            Console.WriteLine("1. You don't exercise");
            Console.WriteLine("2. You engage in light exercise one to three days a week");
            Console.WriteLine("3. You exercise moderately three to 5 times a week");
            Console.WriteLine("4. You exercise intensely six to seven days a week");
            Console.WriteLine("5. You exercise intensely six to seven days a week " +
                "and have a physically active job");
            Console.WriteLine ("6. QUIT");
        }

        static void ProcessChoice (int c)
        {
            double allowedCalories;
            if (c == 1) {
                allowedCalories = BMR * 1.2;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 2) {
                allowedCalories = BMR * 1.375;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 3) {
                allowedCalories = BMR * 1.55;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 4) {
                allowedCalories = BMR * 1.725;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 5) {
                allowedCalories = BMR * 1.9;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            }
        }
    }
}

MBRmain 中本地声明。您将无法在 main 方法之外直接使用它。

有多种方法可以解决这个问题,但由于您的代码由单个 class 组成(不需要在 class 之间进行依赖注入),我想到了两种主要方法:


第一种方式:

您可以在更高的范围级别声明它(在本例中,MainClass):

class MainClass
{
    double MBR = 0;
    //...

这使得整个 class 都可以访问该变量,包括其中的方法,而后者又包括 ProcessChoice.


第二种方式:

您可以将其作为参数传递给ProcessChoice

static void ProcessChoice (int c, double MBR) {
    //...

int choice;
do {
    PrintMenu ();
    choice = Int32.Parse (Console.ReadLine ());
    ProcessChoice(choice, MBR);
} //...

您需要将 BMR 值传递给 ProcessChoice 函数:

    ProcessChoice (choice, BMR);

    static void ProcessChoice (int c, double BMR)
    {
         ....
    }