用C#言语利用控制台编写一个四则运算的C#程序(求和、差、积和商)
展开全部
您好!这段代码我也刚写完,看看对您是否有帮助
这个四则运算程序是循环的,并且运算符输错会提示重新输入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 计算器
{
class Program
{
static void Main(string[] args)
{
int x = 1;
do
{
Console.Write("请输入第一个数字:");
double a = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入第二个数字:");
double b = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入运算符:");
string c = Console.ReadLine();
if (c == "+" || c == "-" || c == "*" || c == "/")
{
switch (c)
{
case "+":
Console.WriteLine("" + a + "" + c + "" + b + "=" + (a + b));
break;
case "-":
Console.WriteLine("" + a + "" + c + "" + b + "=" + (a - b));
break;
case "*":
Console.WriteLine("{0}{1}{2}={3}", a, c, b, (a * b));
break;
case "/":
Console.WriteLine("{0}{1}{2}={3}", a, c, b, (a / b));
break;
}
}
/*else if (!IsNum(a))
{
Console.WriteLine("您的输入有错!请重新输入(*^__^*) 嘻嘻……");
x = 1;
* }*/
else
{
Console.WriteLine("您的输入有错!请重新输入(*^__^*) 嘻嘻……");
x = 1;
}
} while (x ==1);
}
}
这个四则运算程序是循环的,并且运算符输错会提示重新输入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 计算器
{
class Program
{
static void Main(string[] args)
{
int x = 1;
do
{
Console.Write("请输入第一个数字:");
double a = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入第二个数字:");
double b = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入运算符:");
string c = Console.ReadLine();
if (c == "+" || c == "-" || c == "*" || c == "/")
{
switch (c)
{
case "+":
Console.WriteLine("" + a + "" + c + "" + b + "=" + (a + b));
break;
case "-":
Console.WriteLine("" + a + "" + c + "" + b + "=" + (a - b));
break;
case "*":
Console.WriteLine("{0}{1}{2}={3}", a, c, b, (a * b));
break;
case "/":
Console.WriteLine("{0}{1}{2}={3}", a, c, b, (a / b));
break;
}
}
/*else if (!IsNum(a))
{
Console.WriteLine("您的输入有错!请重新输入(*^__^*) 嘻嘻……");
x = 1;
* }*/
else
{
Console.WriteLine("您的输入有错!请重新输入(*^__^*) 嘻嘻……");
x = 1;
}
} while (x ==1);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询