具有三个公共属性的3个私有双重数据成员的加法计算器

时间:2018-09-10 01:36:20

标签: c#

计算器版本1

创建控制台项目。

将“ Program.cs”重命名为“ CalculatorRun.cs”

添加类“ Calculator.cs” -具有三个公共属性的3个私有双重数据成员 -firstOperand,FirstOperand; -secondOperand,SecondOperand; -结果,结果;

  • 1个公共方法Addition()
  • 使用这种方法进行加法,结果= FirstOperand + SecondOperand;

在“ CalculatorRun.cs”中,让用户输入firstOperand和secondOperand的值, 显示加法结果。

我已经研究了几个小时,这仍然没有意义。我的代码没用。有人输入吗?

这就是我拥有的:

namespace CalculatorRun
{
    class Calculator
    {
        static void Main(string[] args)
        {
            decimal FirstOperand, SecondOperand, Result, result;
            Console.Write("Addition Calculation");
            Console.Write("      \n\n");
            Console.Write("   Enter first operand:       ");
            FirstOperand = Convert.ToDecimal(Console.ReadLine());
            Console.Write("   Enter second operand:      ");
            SecondOperand = Convert.ToDecimal(Console.ReadLine());
            Console.Write("-------------------------\n\n");
            Result = FirstOperand + SecondOperand;
            result = Convert.ToDecimal(Result);
            Console.WriteLine("Addition Result: {0}", string.Format("{0}", result));
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }
    }
}

但是它需要更像这样(当然使用顶部的指示)。但是,当我尝试过时,我无法使它正常工作。 :     使用系统;

namespace Calculator
{
    class Program
    {
        public static void Main(string[] args)
        {

        }//<-----------

        public int number01;
        public int number02;
        public int Number03
        {
            get
            {
                return number02 / number01;
            }
        }//<----------

        class Program1 : Program
        {

            public void DivideFinal()//<---- void not int
            {
                Console.Write("Enter a number to be divided: ");
                Console.ReadKey();
                number01 = Convert.ToInt32(Console.ReadKey());
                Console.WriteLine("Enter another number to be divided");
                number02 = Convert.ToInt32(Console.ReadKey());
                Console.WriteLine("The result is: " + Number03);
                Console.ReadKey();
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

这将为您完成。注释中的解释:

using System;

namespace ConsoleApplication1 {
    class Program {

        static void Main( string[] args ) {
            Calculator calculator = new Calculator();

            Console.Write( "   Enter first operand:       " );

            calculator.FirstOperand = Convert.ToDouble( Console.ReadLine() ); //get the first

            Console.Write( "   Enter first operand:       " );

            calculator.SecondOperand = Convert.ToDouble( Console.ReadLine() ); //get the second

            calculator.Addition(); //Do addition

            Console.WriteLine( "Addition Result: {0}", calculator.Result ); //show result

            Console.WriteLine( "Press any key to continue....." );
            Console.ReadLine();
        }

    }

    class Calculator {
        private double firstOperand, secondOperand, result; //double data members

        //3 public properties
        public double FirstOperand {
            get { return firstOperand; }
            set { firstOperand = value; }
        }

        public double SecondOperand {
            get { return secondOperand; }
            set { secondOperand = value; }
        }

        public double Result {
            get { return result; }
            set { result = value; }
        }

        //1 public method Addition()
        public double Addition() {
            return result = firstOperand + secondOperand;
        }

    }

}