TryParse not working and if else condition not working - c#

I am trying to make a normal form where I can convert Celsius toFahrenheit or viceversa.
private void button1_Click(object sender, EventArgs e)
{
double celsius;
double fahrenheit;
string value;
double finalValue;
bool successCelsius = double.TryParse(textBox1.Text, out celsius);
bool successFahrenheit = double.TryParse(textBox2.Text, out fahrenheit);
if (successCelsius == false)
{
label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
return;
}
else
{
celsius = Convert.ToDouble(textBox1.Text);
finalValue = (celsius * 9) / 5 + 32;
label6.Text = finalValue.ToString();
label6.Text = celsius + " " + "degrees Celsius converts to" + " " + finalValue + " " + "degrees Fahrenheit";
}
if (successFahrenheit == false)
{
label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
return;
}
else
{
fahrenheit = Convert.ToDouble(textBox2.Text);
finalValue = (fahrenheit - 32) * 5 / 9;
value = finalValue.ToString();
label6.Text = fahrenheit + " " + "degrees Fahrenheit converts to" + " " + finalValue + " " + "degrees Celsius";
}
}
Textbox accepted the values and displays to label6. It checks if the entered value is a double or not and then it works.
idk why tryParse isnt working.
Can you help me out? :)

The reason this code doesn't "work" is because of your code flow.
The way the code is structured right now, you have to enter both a celsius and a farenheit value or your code will display an "ERROR". If textbox1 or textbox2 are empty, your code will return false from TryParse. If you follow that logic, this code will execute (if you only enter celsius):
if (successFahrenheit == false)
{
label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
return;
}
UPDATE:
You need to handle both scenarios separately...here is one way that is simple....check for one and do it....otherwise, check the other. THere are a lot of ways to "solve" this problem but this would be one of the easiest.
private void button1_Click(object sender, EventArgs e)
{
double celsius;
double fahrenheit;
string value;
double finalValue;
if (textBox1.Text.Length > 0)
{
bool successCelsius = double.TryParse(textBox1.Text, out celsius);
if (successCelsius == false)
{
label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
return;
}
else
{
celsius = Convert.ToDouble(textBox1.Text);
finalValue = (celsius * 9) / 5 + 32;
label6.Text = finalValue.ToString();
label6.Text = celsius + " " + "degrees Celsius converts to" + " " + finalValue + " " + "degrees Fahrenheit";
}
}
else
{
bool successFahrenheit = double.TryParse(textBox2.Text, out fahrenheit);
if (successFahrenheit == false)
{
label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
return;
}
else
{
fahrenheit = Convert.ToDouble(textBox2.Text);
finalValue = (fahrenheit - 32) * 5 / 9;
value = finalValue.ToString();
label6.Text = fahrenheit + " " + "degrees Fahrenheit converts to" + " " + finalValue + " " + "degrees Celsius";
}
}
}

Related

Convert mm (metric) to imperial (fraction) in c#

public class Convert
{
public void MMtofeetfraction(float millimeters)
{
if ((millimeters % 304.8f) != 0)
{
int feet = (int)(millimeters / 304.8f);
double inchWithDecimal = millimeters * 0.03937;
string s = inchWithDecimal.ToString("0.0000");
string[] parts = s.Split('.');
int i1 = int.Parse(parts[0]);
float i2 = float.Parse("0." + parts[1]);
int inch = i1 % 12;
var accuracy = 16;
double num = 1;
double frac = i2 * accuracy;
num = Math.Round(frac);
if (num != 0)
{
while (num % 2 == 0)
{
num = num / 2;
accuracy = accuracy / 2;
}
if (inch != 0)
{
Debug.Log("Fraction output: " + feet + "'-" + inch + " " + num + "/" + accuracy + "\"");
}
else
{
Debug.Log("Fraction output: " + feet + "'-" + num + "/" + accuracy + "\"");
}
}
else
{
Debug.Log("Fraction output: " + feet + "'-" + inch + "\"");
}
}
else
{
Debug.Log("Fraction output: " + Math.Round(millimeters / 304.8) + "'");
}
}
}
Input 1440 : output : 4' 7 1/2"
Input 1550 : output : 5' 1 "
Input 1530 : output : 5' 1/4"
Now, this code works and gives output as needed. I was looking to improvise this code by removing string splitting and other string formatting. Can someone help with inbuilt functions to handle this?

A local or parameter named 'Result' cannot be declared in this scope (Error CS0136)

I am learning c# and I'm trying to make a program that calculate the champions winrate in (lol) League of Legends and I'm stuck with this problem:
The error is:
Error CS0136 A local or parameter named 'Result' cannot be declared in
this scope because that name is used in an enclosing local scope to
define a local or parameter
Here is my code:
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
double TeemoWinRate = 51.33;
double AkaliWinRate = 47.56;
double YasuoWinRate = 49.73;
double KhazixWinRate = 50.94;
double YoneWinRate = 47.84;
double LuxWinRate = 50.50;
double MorgWinRater = 50.83;
double TrandyWinRate = 48.50;
double GarenWinRate = 50.50;
double TeemoGold = 11.113;
double AkaliGold = 10.880;
double YasuoGold = 11.828;
double KhazixGold = 11.541;
double YoneGold = 11.650;
double LuxGold = 9.497;
double MorgGold = 8.990;
double TrandyGold = 12.182;
double GarenGold = 11.609;
double Te = TeemoWinRate;
double A = AkaliWinRate;
double Y = YasuoWinRate;
double K = KhazixWinRate;
double Yo = YoneWinRate;
double L = LuxWinRate;
double M = MorgWinRater;
double Tr = TrandyWinRate;
double G = GarenWinRate;
Console.WriteLine("Champions Ranked WinRate From the list below");
Console.WriteLine("Akali, " +
"Yasuo, " +
"Khazix, " +
"Yone, " +
"Lux, " +
"Morg, " +
"Trandy, " +
"Garen, " +
"Teemo.");
Console.WriteLine("Note: get sure that names are written as the list");
Console.Write("Enter Champion Name : ");
string Champion = Console.ReadLine();
//------------------------------First if stattement--------------------------------------------
if (Champion == "Akali")
{
Console.WriteLine("Akali WinRate is: " + AkaliWinRate + " " + "Total Gold: " + AkaliGold);
}
else if (Champion == "Yasuo")
{
Console.WriteLine("Yasuo WinRate is: " + YasuoWinRate + " " + "Total Gold: " + YasuoGold);
}
else if (Champion == "Khazix")
{
Console.WriteLine("Khazix WinRate is: " + KhazixWinRate + " " + "Total Gold: " + KhazixGold);
}
else if (Champion == "Yone")
{
Console.WriteLine("Yone WinRate is: " + YoneWinRate + " " + "Total Gold: " + YoneGold);
}
else if (Champion == "Lux")
{
Console.WriteLine("Lux WinRate is: " + LuxWinRate + " " + "Total Gold: " + LuxGold);
}
else if (Champion == "Morg")
{
Console.WriteLine("Morg WinRate is:" + MorgWinRater + " " + "Total Gold: " + MorgGold);
}
else if (Champion == "Trandy")
{
Console.WriteLine("Trandy WinRate is: " + TrandyWinRate + " " + "Total Gold: " + TrandyGold);
}
else if (Champion == "Garen")
{
Console.WriteLine("Garen WinRate is: " + GarenWinRate + " " + "Total Gold: " + GarenGold);
}
else if (Champion == "Teemo")
{
Console.WriteLine("Teemo WinRate is" + TeemoWinRate + " " + "Total Gold: " + TeemoGold);
}
else
{
Console.WriteLine("Please Enter the name Exactly as in the list.");
return;
}
Console.WriteLine("what champion do u want to Compare with other champs");
Console.WriteLine("Type " +
"\n 1 for (Teemo) " +
"\n 2 for (Akali) " +
"\n 3 for (Yasuo) " +
"\n 4 for (khazix) " +
"\n 5 for (Yone) " +
"\n 6 for (Lux)" +
"\n 7 for (Morg)" +
"\n 8 for (Trandy)" +
"\n 9 for (Garen)");
Console.Write("First Champion: ");
double Answer1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Second Champion");
double Answer2 = Convert.ToInt32(Console.ReadLine());
//------------------------------Second if stattement--------------------------------------------
if (Answer1 == 1)
{
object Result = Answer1 - 1 + Te;
}
if (Answer1 == 2)
{
object Result = Answer1 - 2 + A;
}
if (Answer1 == 3)
{
object Result = Answer1 - 3 + Y;
}
if (Answer1 == 4)
{
object Result = Answer1 - 4 + K;
}
if (Answer1 == 5)
{
object Result = Answer1 - 5 + Yo;
}
if (Answer1 == 6)
{
object Result = Answer1 - 6 + L;
}
if (Answer1 == 7)
{
object Result = Answer1 - 7 + M;
}
if (Answer1 == 8)
{
object Result = Answer1 - 8 + Tr;
}
if (Answer1 == 9)
{
object Result = Answer1 - 9 + G;
}
//-----------------------------------Third if Statement--------------------------
if (Answer2 == 1)
{
object Result2 = Answer2 - 1 + Te;
}
if (Answer2 == 2)
{
object Result2 = Answer2 - 2 + A;
}
if (Answer2 == 3)
{
object Result2 = Answer2 - 3 + Y;
}
if (Answer2 == 4)
{
object Result2 = Answer2 - 4 + K;
}
if (Answer2 == 5)
{
object Result2 = Answer2 - 5 + Yo;
}
if (Answer2 == 6)
{
object Result2 = Answer2 - 6 + L;
}
if (Answer2 == 7)
{
object Result2 = Answer2 - 7 + M;
}
if (Answer2 == 8)
{
object Result2 = Answer2 - 8 + Tr;
}
if (Answer2 == 9)
{
object Result2 = Answer2 - 9 + G;
}
}
}
}
The error says that you cannot use Result since it is being used somewhere else try renaming it.

Why if the result if correct it tells me that is wrong [duplicate]

This question already has answers here:
Rule of thumb to test the equality of two doubles in C#?
(6 answers)
How should I compare these doubles to get the desired result?
(7 answers)
Arithmetic error when adding two double values [duplicate]
(3 answers)
Closed 3 years ago.
This program bring to random numbers in a decimal way to get sum using ran.next and are stored in the variables sum1a (for the first number) and sum2a (for the second number) and printed in "Suma1.Text"; the operator need to captured the result in the textbox called "Resultado" how has the name "Resultado1" and stored in the variable result1, then is compared with the sum of sum1a and sum2a and stored in total1 to get compared with result1, if total1 match with result1 have to print "Correcto" or "Incorrecto" when there is no match, but sometimes even when the result is correct said that is "Incorrecto".
[Calculator vs Program][1]
namespace Sumas_punto_decimal_prueba
{
public partial class Form1 : Form
{
double sum1a, sum2a;
string num1a, num2a;
double total1;
double result1;
public Form1()
{
InitializeComponent();
}
private void Generar_Click(object sender, EventArgs e)
{
//LIMPIADOR
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
TextBox text = ctrl as TextBox;
text.Clear();
}
}
Random ran = new Random();
//SUMA 1
sum1a = ran.Next(100 , 10000) / 100.00;
sum2a = ran.Next(100 , 10000) / 100.00;
num1a = sum1a.ToString("##.#0");
num2a = sum2a.ToString("##.#0");
if (sum1a < 10 && sum2a < 10)
{
Suma1.Text = " 0" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1 && sum2a < 1)
{
Suma1.Text = " 00" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 10 && sum2a < 1)
{
Suma1.Text = " 0" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1 && sum2a < 10)
{
Suma1.Text = " 00" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 10)
{
Suma1.Text = " 0" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1)
{
Suma1.Text = " 00" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 10)
{
Suma1.Text = " " + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 1)
{
Suma1.Text = " " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 1)
{
Suma1.Text = " " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else
{
Suma1.Text = " " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
}
private void Calificar_Click(object sender, EventArgs e)
{
//SUMA NUMERO 1
double.TryParse(Resultado1.Text, out result1);
total1 = sum1a + sum2a;
if (result1 == total1)
{
Validacion1.Text = "Correcto";
}
else
{
Validacion1.Text = "Incorrecto. \r\nEl resultado es: " + total1;
}
}
}
}
What can be the reason that sometimes that error happen? I make a stimated and happen 1 of 15 times.
Thank to all for the help but already found a solution, using Math.Round() and "Zero placeholder"
sum1a = ran.Next(10000) / 100.00;
sum2a = ran.Next(10000) / 100.00;
sum1a = Math.Round(sum1a, 2, MidpointRounding.AwayFromZero);
sum2a = Math.Round(sum2a, 2, MidpointRounding.AwayFromZero);
num1a = sum1a.ToString("00.00");
num2a = sum2a.ToString("00.00");
Suma1.Text = " " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
Thanks. uwu
Probably because they don't even equal at all,
Probably not all the numbers were captured in. Compare
The strings instead and use print action to see where the error is at
if (string == string)
{
// Ganastes!
}
if("159.73" == "159.73")
{
// Correct!
}

How do I keep the previous equations from being displayed

I have a project where the user can insert a math equation in a single line using the split command. In the first loop the equation get solved correctly and displays the equation with the answer. The problem is with the second loop. It solves the equation and displays the equation with the answer but it also displays the equation from before.
using System;
namespace Calculator1
{
class Program
{
static void Main(string[] args)
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
while (Continue)
{
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
}
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
if (response == "Yes")
{
Console.WriteLine("\nEnter your equation below:");
string[] values1 = Console.ReadLine().Split(' ');
double firstNum1 = double.Parse(values1[0]);
string operation1 = (values1[1]);
double secondNum1 = double.Parse(values1[2]);
if (operation1 == "*")
{
answer1 = firstNum1 * secondNum1;
Console.WriteLine("\n" + firstNum1 + " * " + secondNum1 + " = " + answer1);
}
else if (operation1 == "/")
{
answer1 = firstNum1 / secondNum1;
Console.WriteLine("\n" + firstNum1 + " / " + secondNum1 + " = " + answer1);
}
else if (operation1 == "+")
{
answer1 = firstNum1 + secondNum1;
Console.WriteLine("\n" + firstNum1 + " + " + secondNum1 + " = " + answer1);
}
else if (operation1 == "-")
{
answer1 = firstNum1 - secondNum1;
Console.WriteLine("\n" + firstNum1 + " - " + secondNum1 + " = " + answer1);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
}
else
{
Continue = false;
}
}
}
}
}
Output:
Math Operations:
--------------------
Multiplication: *
Addition: +
Subtraction: -
Division: /
Enter your equation below:
For example: 5 + 5
5 + 5
5 + 5 = 10
Do you want to continue?
Type in Yes or No:
Yes
Enter your equation below:
10 + 10
10 + 10 = 20
5 + 5 = 10 \\ I don't want this to appear
Do you want to continue?
Type in Yes or No:
All you need to do is put the code that gets the user input into the while loop, and then let the loop continue if the user answers "Yes" at the end of the loop:
private static void Main()
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
}
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
if (response != "Yes") Continue = false;
}
}
Your Issue:
Its because you have the output code twice in the loop and both of them are displaying different variables.
Solution:
namespace Calculator1
{
class Program
{
static void Main(string[] args)
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
} else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
} else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
} else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
} else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
Continue = (response == "Yes");
}
}
}
}

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

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);
}
}
}
}
MBR is declared locally in main. You will not be able to use it outside of the main method in a direct manner.
There are multiple ways to solve this issue, but since your code consists of a single class (no dependency-injection between classes is necessary), two main ways come to mind:
First way:
You can declare it at a higher scope level (in this case, MainClass):
class MainClass
{
double MBR = 0;
//...
This makes the variable accessible to the entire class, including methods in it, which in turn includes ProcessChoice.
Second way:
You can pass it to ProcessChoice as a parameter:
static void ProcessChoice (int c, double MBR) {
//...
and
int choice;
do {
PrintMenu ();
choice = Int32.Parse (Console.ReadLine ());
ProcessChoice(choice, MBR);
} //...
You need to pass the BMR value to your ProcessChoice function:
ProcessChoice (choice, BMR);
static void ProcessChoice (int c, double BMR)
{
....
}

Categories

Resources