如何从一个条件访问信息,稍后在另一个条件中使用它?

时间:2017-10-23 21:43:34

标签: c# loops 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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:4)

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

有多种方法可以解决这个问题,但由于您的代码由一个类组成(类之间不需要依赖注入),因此可以想到两种主要方式:

第一种方式:

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

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

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

第二种方式:

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

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

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

答案 1 :(得分:1)

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

    ProcessChoice (choice, BMR);

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