I'm trying to access the answer from an equation (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);
and use it here
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);
}
But I keep getting errors.
Here's the entire code:
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);
}
}
}
}
BMR
is defined inside ofMain
, it is not accessible outside of that method. You could declare it outside of main, at the class level:private double BMR = 0;
, which allows all methods inside the class to access it, or you can include it in the method signature of any method that needs it:static void ProcessChoice(int c, double BMR)
ProcessChoice
function as well.