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

计算器版本 1

创建一个控制台项目。

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

添加类“Calculator.cs” - 具有三个公共属性的 3 个私有双数据成员 - firstOperand、FirstOperand;- 第二操作数,第二操作数;- 结果,结果;

  • 1 公共方法 Addition()

  • 在这个方法中做加法,Result = 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;

            }

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

    }

}


缥缈止盈
浏览 143回答 1
1回答

哆啦的时光机

这将为您完成。评论中的解释:using System;namespace ConsoleApplication1 {&nbsp; &nbsp; class Program {&nbsp; &nbsp; &nbsp; &nbsp; static void Main( string[] args ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Calculator calculator = new Calculator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write( "&nbsp; &nbsp;Enter first operand:&nbsp; &nbsp; &nbsp; &nbsp;" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculator.FirstOperand = Convert.ToDouble( Console.ReadLine() ); //get the first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write( "&nbsp; &nbsp;Enter first operand:&nbsp; &nbsp; &nbsp; &nbsp;" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculator.SecondOperand = Convert.ToDouble( Console.ReadLine() ); //get the second&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculator.Addition(); //Do addition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine( "Addition Result: {0}", calculator.Result ); //show result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine( "Press any key to continue....." );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class Calculator {&nbsp; &nbsp; &nbsp; &nbsp; private double firstOperand, secondOperand, result; //double data members&nbsp; &nbsp; &nbsp; &nbsp; //3 public properties&nbsp; &nbsp; &nbsp; &nbsp; public double FirstOperand {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return firstOperand; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { firstOperand = value; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public double SecondOperand {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return secondOperand; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { secondOperand = value; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public double Result {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return result; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { result = value; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //1 public method Addition()&nbsp; &nbsp; &nbsp; &nbsp; public double Addition() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result = firstOperand + secondOperand;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP