Hello, I'm working on an assignment for my intro to programming class where we have to create a BMI calculator and then return the user's allowed calorie intake. When trying to run my code I get the error saying The variable 'activityLevel' is assigned but never used. I'm a little confused because I thought I was using it on lines 41 and 44. I'm sure I'm doing something wrong, so if someone could help me correct my errors I would greatly appreciate it! Code (CSharp): using System; //Austin Beyers //03/21/18 //Lab 05 namespace Lab_02 { class MainClass { static double activityLevel; public static void Main (string[] args) { Console.WriteLine ("Hello and welcome to the BMR Calculator!"); Console.Write ("To begin, please enter your Name: "); string name; name = Console.ReadLine (); Console.Write ("Thanks " + name + "! Now please enter your gender (m or f): "); char gender; gender = char.Parse (Console.ReadLine ()); Console.WriteLine ("Thanks. Now please enter the following information"); Console.Write ("Age: "); int age; age = Int32.Parse (Console.ReadLine ()); Console.Write ("Weight (in pounds): "); int weight; weight = Int32.Parse (Console.ReadLine ()); Console.Write ("Height (in inches): "); int height; height = Int32.Parse (Console.ReadLine ()); int mBMR; mBMR = Convert.ToInt32 (66 + (6.23 * weight) + (12.7 * height) - (6.8 * age)); int fBMR; fBMR = Convert.ToInt32 (655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)); PrintMenu (); int exerciseFactor = Int32.Parse(Console.ReadLine()); ProcessChoice(exerciseFactor); if (gender == 'm') { Console.WriteLine ("Your BMR is " + mBMR + ", and your allowed calories per day is " + (mBMR * activityLevel)); } if (gender == 'f') { Console.WriteLine ("Your BMR is " + fBMR + ", and your allowed calories per day is " + (fBMR * activityLevel)); } } public static void PrintMenu() { Console.WriteLine ("What is your current Activity Level?"); 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 five 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"); } static void ProcessChoice (int ef) { if (ef == 1) { double activityLevel; activityLevel = 1.2; } else if (ef == 2) { double activityLevel; activityLevel = 1.375; } else if (ef==3){ double activityLevel; activityLevel = 1.55; } else if (ef==4){ double activityLevel; activityLevel = 1.725; } else if (ef==5){ double activityLevel; activityLevel = 1.9; } } } }
This forum is specific to Unity C# Scripting, you should ask this kind of generic questions on other forums. Anyway, the problem in your code is that you have declared a static variable on line 11, and then you are declaring local variables with the same name in lines 62,65,68,71,74. The local variables are overriding the static variable in those contexts, so when you assign a value to that local variable, the compiler tells you that you are not using the local variables (you are just assigning values). The solution colud be to remove the lines 62,65,68,71,74, so the assigned variable is the static one declared in line 11.
Also, don't use static variables. You just learned somewhat why. Since you are new to programming just go ahead and get in the habit of not using them. Static variables are the lazy way to do something and can ruin an entire project if not done correctly. It also makes it extremely difficult to troublshoot as your project grows. From now own for just general coding questions that have nothing to do with Unity, use this site instead. https://stackoverflow.com