尝试做一个计算器

Trying to make a calculator

所以基本上每当我尝试更改 while 循环内的变量“restart”时,它都会给我一个错误,提示“无法在此范围内声明名为 'restart' 的局部参数或参数,因为该名称已被使用在封闭的本地范围内定义本地或参数”。如果有人知道如何解决这个问题,请告诉我


public class Program
{
    public static void Main()
    {
        bool restart = true;
        while (restart == true)
        {
            Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
            string Method = Console.ReadLine();
            if (Method == "Add")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne + numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Subtract")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne - numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Multiply")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne * numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Divide")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne / numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else
            {
                Console.WriteLine("error found");
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
        }
    }
}```

让我们看看您的这部分代码:

bool restart = true;
while (restart == true)
{
    // Some stuff omitted...
    bool restart = Convert.ToBoolean(Console.ReadLine());
}

重新申报restart。相反,您应该像这样分配它(在首次声明变量后使用变量时将 bool 关闭):

restart = Convert.ToBoolean(Console.ReadLine());

你需要这样的东西:

    bool restart = true;
    while (restart)
    {
        Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
        string method = Console.ReadLine();
        if (new[] { "Multiply", "Divide", "Add", "Subtract" }.Contains(method))
        {
            Console.WriteLine("Please Enter First Number");
            double numberOne = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please Enter The Second Number");
            double numberTwo = Convert.ToDouble(Console.ReadLine());
            if (method == "Add")
            {
                Console.WriteLine(numberOne + numberTwo);
            }
            else if (method == "Subtract")
            {
                Console.WriteLine(numberOne - numberTwo);
            }
            else if (method == "Multiply")
            {
                Console.WriteLine(numberOne * numberTwo);
            }
            else if (method == "Divide")
            {
                Console.WriteLine(numberOne / numberTwo);
            }
            Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
            restart = Convert.ToBoolean(Console.ReadLine());
        }
        else
        {
            Console.WriteLine("error found");
            Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
            restart = Convert.ToBoolean(Console.ReadLine());
        }
    }

或者这样:

    Dictionary<string, Func<double, double, double>> operations =
        new Dictionary<string, Func<double, double, double>>()
        {
            { "Add", (x, y) => x + y },
            { "Subtract", (x, y) => x - y },
            { "Multiply", (x, y) => x * y },
            { "Divide", (x, y) => x / y },
        };
    
    bool restart = true;
    while (restart)
    {
        Console.WriteLine($"Please enter one of: {String.Join(", ", operations.Keys)}");
        string method = Console.ReadLine();
        if (operations.ContainsKey(method))
        {
            Console.WriteLine("Please Enter First Number");
            double numberOne = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please Enter The Second Number");
            double numberTwo = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine(operations[method](numberOne, numberTwo));
        }
        else
        {
            Console.WriteLine("error found");
        }
        Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
        restart = Convert.ToBoolean(Console.ReadLine());
    }