C#题编写一个进行加减乘除四则运算的程序,要求;输出2个单精度数,然后输入一个运算符号,输出结果
2014-11-27
展开全部
新鲜出炉的,来吧。
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询