用c#中的委托,实现两个实数的加减乘除运算
2个回答
2018-04-09
展开全部
y on the kitchen wa
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Program
{
static void Main(string[] args)
{
while (true)
{
Calcu();
Console.WriteLine("是否继续执行循环?Y/N");
string s = Console.ReadLine();
if (s.ToUpper() == "Y")
{
Calcu();
continue;
}
break;
}
}
static void Calcu()
{
Console.WriteLine("请输入第一个数字:");
float num1 = 0;
float num2 = 0;
string fuhao = "";
while (true)
{
try
{
num1 = Convert.ToSingle(Console.ReadLine());
break;
}
catch (Exception)
{
num1 = 0;
Console.WriteLine("输入错误,请重新输入第一个数字:");
}
}
Console.WriteLine("请输入第二个数字:");
while (true)
{
try
{
num2 = Convert.ToSingle(Console.ReadLine());
break;
}
catch (Exception)
{
num2 = 0;
Console.WriteLine("输入错误,请重新输入第二个数字:");
}
}
Console.WriteLine("请输入运算符(+ - * /):");
while (true)
{
try
{
fuhao = Console.ReadLine();
if (fuhao == "+" || fuhao == "-" || fuhao == "*" || fuhao == "/")
break;
fuhao = "";
Console.WriteLine("运算符输入错误,请重新输入运算符(+ - * /):");
}
catch (Exception)
{
fuhao = "";
Console.WriteLine("运算符输入错误,请重新输入运算符(+ - * /)");
}
}
float num = 0;
switch (fuhao)
{
case "+": num = num1 + num2; break;
case "-": num = num1 - num2; break;
case "*": num = num1 * num2; break;
case "/": num = num1 / num2; break;
}
Console.WriteLine("运算结果出炉: "+num1 + fuhao + num2 + "=" + num);
}
}
{
static void Main(string[] args)
{
while (true)
{
Calcu();
Console.WriteLine("是否继续执行循环?Y/N");
string s = Console.ReadLine();
if (s.ToUpper() == "Y")
{
Calcu();
continue;
}
break;
}
}
static void Calcu()
{
Console.WriteLine("请输入第一个数字:");
float num1 = 0;
float num2 = 0;
string fuhao = "";
while (true)
{
try
{
num1 = Convert.ToSingle(Console.ReadLine());
break;
}
catch (Exception)
{
num1 = 0;
Console.WriteLine("输入错误,请重新输入第一个数字:");
}
}
Console.WriteLine("请输入第二个数字:");
while (true)
{
try
{
num2 = Convert.ToSingle(Console.ReadLine());
break;
}
catch (Exception)
{
num2 = 0;
Console.WriteLine("输入错误,请重新输入第二个数字:");
}
}
Console.WriteLine("请输入运算符(+ - * /):");
while (true)
{
try
{
fuhao = Console.ReadLine();
if (fuhao == "+" || fuhao == "-" || fuhao == "*" || fuhao == "/")
break;
fuhao = "";
Console.WriteLine("运算符输入错误,请重新输入运算符(+ - * /):");
}
catch (Exception)
{
fuhao = "";
Console.WriteLine("运算符输入错误,请重新输入运算符(+ - * /)");
}
}
float num = 0;
switch (fuhao)
{
case "+": num = num1 + num2; break;
case "-": num = num1 - num2; break;
case "*": num = num1 * num2; break;
case "/": num = num1 / num2; break;
}
Console.WriteLine("运算结果出炉: "+num1 + fuhao + num2 + "=" + num);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询