我有什么办法可以清理此代码以使其更高效?另外,有人可以指出我该应用程序比我做的更高级的方向吗?
随着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();
}
}
}
答案 0 :(得分:0)
我的评论:
Calculator
类中,不应放置对number3
这样的类无用的变量。goto
。最好使用while
循环。enum
进行操作。