这是用C#语言做的一个控制台的简易计算器
运用了if判断语句和else if 语句
为了在控制台循环执行,在代码的外围加了for的无限循环语句。
for的无限循环语句:
for( ; ; )
{
循环体;
}
通过将for语句中的初始表达式,循环条件,循环后执行的表达式置空,实现无限循环。
using System;
namespace calculator
{
class Program
{
static void Main(string[] args)
{
for ( ; ; )
{
Console.WriteLine("输入第一个数");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("计算符号:(+ - * / %)");
String b = Console.ReadLine();
Console.WriteLine("输入第二个数");
int c = int.Parse(Console.ReadLine());
int d;
if (b == "+")
{
d = a + c;
Console.WriteLine("输出结果为" + d);
}
else if (b == "-")
{
d = a - c;
Console.WriteLine("输出结果为" + d);
}
else if (b == "*")
{
d = a * c;
Console.WriteLine("输出结果为" + d);
}
else if (b == "/")
{
d = a / c;
Console.WriteLine("输出结果为" + d);
}
else if (b == "%")
{
d = a % c;
Console.WriteLine("输出结果为" + d);
}
}
}
}
}
运行结果:
输入第一个数
5
计算符号:(+ - * / %)
*
输入第二个数
5
输出结果为25
输入第一个数
4
计算符号:(+ - * / %)
+
输入第二个数
6
输出结果为10
输入第一个数