c#中的If语句不能正确返回方法方程

本文关键字:返回 方法 方程 不能 中的 If 语句 | 更新日期: 2023-09-27 17:50:29

我正在敲我的头试图弄清楚为什么我不能让我的方程不返回为零,因为似乎出于某种原因,MathOperations方法没有做那里的工作?如果有人能帮忙,我将不胜感激。提前感谢!

class MathUI
        {
            public void PromptForInt()
            {
                MathOperations ops = new MathOperations();
                Console.WriteLine("Enter first number to calculate");
                ops.Operand1 = int.Parse(Console.ReadLine());
                Console.WriteLine("'nEnter second number to calculate");
                ops.Operand2 = int.Parse(Console.ReadLine());
                return;
            }
            public void PromptForChoice()
            {
                int choice;
                MathOperations result = new MathOperations();
                Console.WriteLine("'nWhat type of operation would you like to perform?");
                Console.WriteLine("[1] Add 'n[2] Subtract 'n[3] Multiply 'n[4] Divide 'n[5] Exit 'n");
                Console.Write("Enter your choice: ");
                choice = int.Parse(Console.ReadLine());
                if (choice == 1)
                {
                    Console.WriteLine(result.AddNumbers());
                }
                else if (choice == 2)
                {
                    Console.WriteLine(result.SubtractNumbers());
                }
                else if (choice == 3)
                {
                    Console.WriteLine(result.MultiplyNumbers());
                }
                else if (choice == 4)
                {
                    Console.WriteLine(result.DivideNumbers());
                }
                else if (choice == 5)
                {
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("'nInvalid input entered!");
                }
            }
    class MathOperations
        {
            private int operand1;
            private int operand2;
            public int Operand1
            {
                get
                {
                    return operand1;
                }
                set
                {
                    operand1 = value;
                }
            }
            public int Operand2
            {
                get
                {
                    return operand2;
                }
                set
                {
                    operand2 = value;
                }
            }
            public MathOperations()
            {
                operand1 = 0;
                operand2 = 0;
            }
            public int AddNumbers()
            {
                return operand1 + operand2;
            }
            public int SubtractNumbers()
            {
                return operand1 - operand2;
            }
            public float DivideNumbers()
            {
                return (float)operand1 / (float)operand2;  // (float) used to show output with decimal point
            }
            public int MultiplyNumbers()
            {
                return operand1 * operand2;
            }

c#中的If语句不能正确返回方法方程

您正在为操作对象设置operand1和operand2的值,而不是为结果对象设置。

MathOperations ops = new MathOperations();
Console.WriteLine("Enter first number to calculate");
ops.Operand1 = int.Parse(Console.ReadLine());
Console.WriteLine("'nEnter second number to calculate");
ops.Operand2 = int.Parse(Console.ReadLine());

,但在operand1和operand2初始化为0的结果对象上调用方法,因此函数的返回值为0。记住,ops的operand1和operand2保存的是用户的输入,而不是结果的operand1和operand2。您应该使用相同的ops对象来计算结果。