csharp 功能齐全的计算器用C#编写,紧凑和格式化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 功能齐全的计算器用C#编写,紧凑和格式化相关的知识,希望对你有一定的参考价值。
using System;
namespace PA05Local
{
class Calculator
{
static void Main()
{
//Declarations
string sign;
int numberOne;
int numberTwo;
int answer;
sign = GetSign();
numberOne = GetNumberOne();
numberTwo = GetNumberTwo();
answer = PerformCalculation(numberOne, numberTwo, sign);
DisplayAnswer(sign, numberOne, numberTwo, answer);
TerminateProgram();
}
private static int PerformCalculation(int numberOne, int numberTwo, string sign)
{
if (sign == "+")
{
return numberOne + numberTwo;
}
else if (sign == "-")
{
return numberOne - numberTwo;
}
else if (sign == "*")
{
return numberOne * numberTwo;
}
else if (sign == "/")
{
return numberOne / numberTwo;
}
else
{
Console.WriteLine("****INVALID OPERATOR -1 RETURNED****");
return -1;
}
}
private static string GetSign()
{
string sign;
Console.WriteLine("Please enter an operator: <+, -, *, />");
sign = Console.ReadLine();
return sign;
}
private static int GetNumberOne()
{
int numberOne;
Console.WriteLine("Please enter the first number");
numberOne = Convert.ToInt16(Console.ReadLine());
return numberOne;
}
private static int GetNumberTwo()
{
int numberTwo;
Console.WriteLine("Please enter the second number");
numberTwo = Convert.ToInt16(Console.ReadLine());
return numberTwo;
}
private static void DisplayAnswer(string sign, int numberOne, int numberTwo, int answer)
{
Console.WriteLine("{0} {1} {2} = {3}", numberOne, sign, numberTwo, answer);
}
private static void TerminateProgram()
{
Console.WriteLine("When you are finished, press the <ENTER> key to close the window");
Console.ReadLine();
}
}
}
以上是关于csharp 功能齐全的计算器用C#编写,紧凑和格式化的主要内容,如果未能解决你的问题,请参考以下文章
C语言程序设计是啥?
Fledermaus和Surfer合作处理离散和格网数据
在Java语言中四舍五入,使其与C#匹配
csharp 编写一个程序,计算给定数字的阶乘中的尾随零的数量。
csharp 用.Net / c#编写的基本Windows服务
十种常用编程语言特点