注册 登录
编程论坛 C# 论坛

计算加,减,乘,除

冷锋2048 发布于 2015-10-27 15:22, 1777 次点击
C#控制台:从键盘上输入两个整型数据,计算和,差,积,商?我刚刚入门
5 回复
#2
Spy0012015-10-27 22:49
static void Main(string[] args)
{
    Console.Write("a=");
    string a = Console.ReadLine();
    Console.Write("b=");
    string b = Console.ReadLine();
    double x = Convert.ToDouble(a);
    double y = Convert.ToDouble(b);
    Console.WriteLine("a+b=" + (x + y));
    Console.WriteLine("a-b="+ (x-y));
    Console.WriteLine("a*b=" + (x * y));
    Console.WriteLine("a/b=" + x / y);
    Console.ReadKey();
}

#3
over12302015-10-28 08:42
int a, b;
            do
            {
                Console.Write("请输入第一个整数a:");
            } while (!int.TryParse(Console.ReadLine(), out a));
            do
            {
                Console.Write("请输入第二个整数b:");
            } while (!int.TryParse(Console.ReadLine(), out b));
            Console.WriteLine("a+b=" + (a + b).ToString());
            Console.WriteLine("a-b=" + (a - b).ToString());
            Console.WriteLine("a*b=" + (a * b).ToString());
            if (b == 0)
            { Console.WriteLine("因除数b为0,无法进行除法运算!"); }
            else

            { Console.WriteLine("a/b=" + float.Parse(a.ToString()) /float.Parse ( b.ToString())); }

            Console.ReadLine();
#4
冷锋20482015-11-10 17:48
回复 3楼 over1230
谢谢
#5
longhair2015-12-14 18:02
刚接触,学习学习!
#6
ivanchan2015-12-25 11:53
用多态会比较好
1