Csharp : извлечение переменной из одного цикла в другой
У меня возникли проблемы с настройкой калькулятора. Переменная(метод) является символом и вводится в первый цикл while.
во втором цикле while я хочу, чтобы переменная(метод) из первого цикла была перенесена во второй цикл.
но есть ошибка, и она говорит, что переменная не назначена.
пожалуйста помочь. вот этот код:
<pre lang="c#"> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class Program { static void Main(string[] args) { int continu = 0; int continu2 = 0; Console.WriteLine("Welcome To The Alpha Calculator!"); Console.WriteLine("First,Type A Method"); char method; while (continu2 != 0) { Console.Write("Type '+' or '-' or '*' or '/' :"); method = char.Parse(Console.ReadLine()); if (method == '+' || method == '/' || method == '*' || method == '-') { continu = 1; continu2 = 1; Console.WriteLine(method + " Picked"); } else { Console.WriteLine("Unknown Method Try Again"); } char method2 = method; } while (continu != 0) { Console.Write("Pick A First Digit:"); double num1 = double.Parse(Console.ReadLine()); Console.Write("Pick A Second Digit:"); double num2 = double.Parse(Console.ReadLine()); double sum = 0; if (method == '+') { sum = num1 + num2; } else if (method == '-') { sum = num1 - num2; } else if (method == '/') { sum = num1 / num2; } else if (method == '*') { sum = num1 * num2; } Console.WriteLine("The Solution is: " + sum); continu = 0; } } } }
Что я уже пробовал:
Я попытался изменить значения переменных циклов(continu) на 1, но это не сработало.