有什么方法可以为一个简单的控制台计算器清理代码?

时间:2018-12-06 16:22:54

标签: c# console-application

我有什么办法可以清理此代码以使其更高效?另外,有人可以指出我该应用程序比我做的更高级的方向吗?

随着C#的发展,我开始更多地了解关于如何将我的知识实现到实际代码中而失去的某些东西。

创建应用程序的任何起点都可以帮助我记住如何以及何时使用某些代码,例如:封装,接口多态等。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetCalculator

{public class Calculator

    {
        // User Input1
        public int number1;

        // User Input2
        public int number2;

        // User Input3
        public int number3;

        //User Input4
        public char YesOrNo;

        // Input1 property
        public int Input1 { get
            {
                return number1;
            } set {
                number1 = value;
            }
        }
        // Input2 property
        public int Input2 { get
            {
                return number2;
            } set {
                number2 = value;
            }
        }
        // Input3 property
        public int Input3
        {
            get
            {
                return number3;
            }
            set { 
                number3 = value;
            }
        }
        // Input4 property
        public char Input4
        {
            get
            {
                return YesOrNo;
            }
            set
            {
                YesOrNo = value;
            }
        }
        //Addition
        public void Addition(int number1, int number2)
        {
            Console.WriteLine("\nAddition of the 2 Numbers is " + (number1 + number2));
        }

        //Subtraction
        public void Subtraction(int number1, int number2)
        {
            Console.WriteLine("Subtraction of the 2 Numbers is: " + (number1 - number2));
        }

        //Division
        public void Division(int number1, int number2)
        {
            Console.WriteLine("Division of the 2 Numbers is: " + (number1 / number2));
        }

        //Multiplication
        public void Multiplication(int number1, int number2)
        {
            Console.WriteLine("Multiplication of the 2 Numbers is: " + (number1 * number2));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Calculator calc = new Calculator();
            //Start of the application
            Start:
            Console.WriteLine("\nCalculation of the 2 numbers using 4 different operations");

            //User input with the first number

            Console.Write("\nEnter The first number: ");
            calc.Input1 = Convert.ToInt16(Console.ReadLine());

            //User input with the second number

            Console.Write("Enter the second number: ");
            calc.Input2 = Convert.ToInt16(Console.ReadLine());

            //User input which is equal to 1 of the 4 math operations

            Console.Write("Press 1 for Addition, Press 2 for Subtraction, Press 3 for Division or Press 4 for Multiplication: ");
            calc.number3 = Convert.ToInt16(Console.ReadLine());

            // if the user input is 1 then call the addition operation

            if (calc.number3 == 1)
            {
                //call addition

                calc.Addition(calc.number1, calc.number2);
            }
            else
            // if the user input is 2 then call the subtraction operation
                if (calc.number3 == 2)
            {
                //call subtraction
                calc.Subtraction(calc.number1, calc.number2);
            }
            else
            // if the user input is 3 then call the division operation
                if (calc.number3 == 3)
            {
                //call division
                calc.Division(calc.number1, calc.number2);
            }
            else
            // if the user input is 4 then call the multiplication operation
                if (calc.number3 == 4)
            {
                //call multiplication 
                calc.Multiplication(calc.number1, calc.number2);
            }

            //User input for starting again or finishing the application
            Console.Write("\nWould you like to start again? Y or N: ");
            calc.YesOrNo = Convert.ToChar(Console.ReadLine());

            //if the user input is equal to Y then send them back to the start of the application
            if (calc.YesOrNo == 'Y')
            {
                goto Start;
            }
            else
            // if the user input is equal to N then send them to the end of the application
                if (calc.YesOrNo == 'N')
            {
                goto End;
            }

            //End of the application
            End:
            //Exit
            Console.WriteLine("\nPress Enter to exit!");
            Console.Read();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我的评论:

  • Calculator类中,不应放置对number3这样的类无用的变量。
  • 没有理由同时拥有公共变量和属性。
  • 没有理由在这样的程序中使用goto。最好使用while循环。
  • 为您的变量找到更好的名称。
  • 您的主要功能应分为多个小功能。
  • 使用enum进行操作。
  • 与您的类型保持一致。鉴于计算器接受32位输入,您可能应该将用户输入转换为32位整数。
  • 可以改善格式
  • 还有更多...