尝试用 C# 制作计算器。这里出了什么问题?

Trying to make a calculator in C#. What is going wrong here?

class Program
{
    static void Main(string[] args)
    {
        bool run = true;
        do
        {
            Console.WriteLine("Make a choice or type 0 to exit: ");
            Console.WriteLine("1. Add 2 numbers\n2. Subtract 2 numbers\n3.Multiply 2 numbers\n4. Divide 2 numbers");
            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 0)
            {
                run = false;
            }
            if(choice == 1)
            {
                int x, y;
                Console.Write("Enter 2 numbers to Operate on: ");
                x = Convert.ToInt32(Console.Read());
                y = Convert.ToInt32(Console.Read());
                Console.WriteLine("The Result is: {0}", Convert.ToInt32(add(x,y)));
            }

        }while(run);
        Console.ReadKey();
    }

    public static int add(int x, int y)
    {
        return x+y;
    }

    public static int sub(int x, int y)
    {
        return x - y;
    }

    public static int mult(int x, int y)
    {
        return x * y;
    }

    public static double div(int x, int y)
    {
        return (float)x / y;
    }

我是 C# 的新手,所以对于补救问题深表歉意。 问题是,当我 运行 并输入 1,然后输入 2 和 4 时,我得到 82,然后菜单被打印两次。这显然是不正确的。有人能告诉我为什么会这样吗?我认为它与为什么我的转换有关,但我想确定为什么语言的行为如此,因为这似乎应该有效。谢谢你的帮助。

编辑:我不确定为什么我被否决了,请让我知道我做错了什么...

这是一个示例输出:

Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
1
Enter 2 numbers to Operate on: 2 4
The Result is: 82
Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers

问题出在这里:

x = Convert.ToInt32(Console.Read());
y = Convert.ToInt32(Console.Read());

你不应该在这里使用 ReadRead 读入单个字符并将其转换为相应的 ASCII 值。基本上,您是将 ASCII 值加在一起。

解决方案:

你直接改成ReadLine:

x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());

但是如果你想让两个数字之间用space隔开,比如2 4,你可以这样做:

string[] numbers = Console.ReadLine().Split(' ');
x = Convert.ToInt32(numbers[0]);
y = Convert.ToInt32(numbers[1]);