C#控制台程序:编写控制台应用程序,迷你简单的计算其。定义名为Cal的类,其中包含两个私有字段n1和n2。编写构造方法,为两个字段初始化。再为该定义加(Addition)、减(Substraction)、乘(Multiplication)、除(Division)4个共有成员方法,分别对其中两个成员变量执行加、减、乘、除的运算。在Main()方法中创建Cal类的对象,调用各个方法,并显示计算结果。(思考:利用窗体程序如何实现?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace text6
{
class Program
{
static void Main(string[] args)
{
double b;
double n1 = 4;
double n2 = 2;
Console.WriteLine("n1={0} n2={1}", n1, n2);
Cal a = new Cal(n1, n2);
Console.WriteLine("相加得;{0}", a.Addition());
Console.WriteLine("相减得;{0}", a.Substraction());
Console.WriteLine("相乘得;{0}", a.Multiplication());
if (!a.Division(out b))
Console.WriteLine("除数不能为零!");
else
{
Console.WriteLine("相除得;{0}", b);
}
Console.ReadLine();
}
}
class Cal
{
private double n1;
private double n2;
public Cal(double n1, double n2)
{
this.n1 = n1;
this.n2 = n2;
}
public double Addition()
{
return n1 + n2;
}
public double Substraction()
{
return n1 - n2;
}
public double Multiplication()
{
return n1 * n2;
}
public bool Division(out double a)
{
if (n2 == 0)
{
a = 0.0 / 0.0;
return false;
}
else
{
a = n1 / n2;
return true;
}
}
}
}