用C#的控制台语句实现加减乘除 简单的…

2024-12-21 11:04:07
推荐回答(4个)
回答1:

using System;
namespace ConsoleApplication1
{
class Program
{
delegate double PD(double p1, double p2);
static double Multiply(double p1, double p2)
{
return p1 * p2;
}
static double Divide(double p1, double p2)
{
return p1 / p2;
}
static double Plus(double p1,double p2)
{
return p1+p2;
}
static double Minus(double p1, double p2)
{
return p1 - p2;
}
static void Main(string[] args)
{
PD pro;
Console.WriteLine("Enter 2 numbers separated withed with args comma:");
string input = Console.ReadLine(); int commaPos = input.IndexOf(',');
double p1 = Convert.ToDouble(input.Substring(0, commaPos));
double p2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - 1 - commaPos));
Console.WriteLine("Enter Mto mutiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
pro = new PD(Multiply);
else if(input=="D")
pro = new PD(Divide);
else if (input == "P") pro = new PD(Plus);
else pro = new PD(Minus);
Console.WriteLine("Result:{0}", pro(p1, p2));
Console.ReadKey();

}
}
}

回答2:

输入的是判断一下是不是数字不就行 了。如果是数字再调用相应函数。不是就不执行,提示再次输入。
public double addition(double i, double j)
{
return i + j;
}

public double subtract(double i, double j)
{
return i - j;
}

public double multiplication(double i, double j)
{
return i * j;
}

public double Division(double i, double j)
{
if (j != 0)
{
return i / j;
}
else
{
return 0;
}
}

回答3:

int i=32+99;
System.Console.WriteLine(i);

回答4:

1楼真简单。