续昨日 学习案例 -希望通过实际的案例帮助初学者
2011-12-13 16:57
447 查看
案例7:
案例8:
案例9:
注:为什么写这个案例是因为以前是计算i%2=0 来判断,现在直接是自增2.
案例11:
案例12:
Math类的方法:
Math.Abs(x) x的绝对值
Math.Ceiling(x) 不小于X的最小整数
Math.Cos(x) x的余弦
Math.Exp(x) 指数函数
Math.Floor(x) 不大于x的最大整数
Math.Max(x,y) 输出最大值 //无法产用3个函数
Math.Min(x,y) 输出最小值
Math.PI 常量 π
注:对于void无返回值的方法,要想输出信息,必须在方法内部自己console!
案例13:
class Program { static void Main(string[] args) { Account account1 = new Account(50.00m); Account account2 = new Account(-7.00m); Console.WriteLine("{0:C}",account1.Balance);//50 Console.WriteLine("{0:C}", account2.Balance); } } class Account { //变量使用前_,属性使用大写开头 private decimal _balance; //私有变量 public Account(decimal i)//构造函数 { Balance = i;//这里是易错的,如果你把Balance写成小写的话,那么下面的if就没用了。。。 } public void Credit(decimal amount)//无返回值方法 { _balance += amount; } public decimal Balance//属性 { get { return _balance; } set { if (value >= 0) { this._balance = value; } } } }
案例8:
class Program { static void Main(string[] args) //Main函数 { Grade one = new Grade("Mr.w");//新建对象,让变量One引用到对象 one.ClassAverge();//调用方法 } } class Grade { public Grade(string name)//构造函数 { Console.WriteLine("welcome for {0}",name); } public void ClassAverge() { int total=0;//总共 int count=0;//计数 double averge=0;//平均数 int number;//分数 for (int i = 0; i <= 10; i++) { total = 0; Console.WriteLine("please enter number:"); number = Convert.ToInt32(Console.ReadLine()); total += number; count=i+1; } averge = total / count; Console.WriteLine("Total is {0}",total); Console.WriteLine("Count is {0}",count); Console.WriteLine("Average is{0}", averge); } }
案例9:
注:为什么写这个案例是因为以前是计算i%2=0 来判断,现在直接是自增2.
static void Main(string[] args) //Main函数 { //求2-20的偶数之和 int total = 0; for (int i = 2; i <= 20; i += 2) { total += i; Console.WriteLine(i+"\t"+total); } Console.WriteLine(total); }
案例11:
class Program { static void Main(string[] args) //Main函数 { FindMax max = new FindMax();//利用对象调用方法 max.Maxi(); Console.WriteLine(FindMax.Maxnum(10,50,60));//直接使用静态函数 } } class FindMax //寻找最大数的类 { public void Maxi() //一个无返回值的方法,这个方法调用了一个内部方法 { double num1; double num2; double num3; Console.WriteLine("please enter 1:"); num1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("please enter 2:"); num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("please enter 3:"); num3 = Convert.ToDouble(Console.ReadLine()); // double result = Maxnum(num1, num2, num3); // Console.WriteLine("Max Number is {0}",result); 2种写法 Console.WriteLine("this is having class:"+FindMax.Maxnum(num1,num2,num3));//这种是在类里直接调用 Console.WriteLine("this is no class:" + Maxnum(num1, num2, num3));//看到没没必要使用类名+ .,因为是在同一个类里 } public static double Maxnum(double a, double b, double c)//设置用静态函数 { double maxnum = a; if (b >= maxnum) { maxnum = b; } if (c >= maxnum) { maxnum = c; } return maxnum; } }
案例12:
Math类的方法:
Math.Abs(x) x的绝对值
Math.Ceiling(x) 不小于X的最小整数
Math.Cos(x) x的余弦
Math.Exp(x) 指数函数
Math.Floor(x) 不大于x的最大整数
Math.Max(x,y) 输出最大值 //无法产用3个函数
Math.Min(x,y) 输出最小值
Math.PI 常量 π
注:对于void无返回值的方法,要想输出信息,必须在方法内部自己console!
案例13:
class Program { static void Main(string[] args) { RefandOut.ROMethod();// 当方法作为static 的时候,引用类的方法不需要使用对象 } } class RefandOut { public static void ROMethod()//这里如果没有括号就是属性,类里 公共方法 如果是 static 那么 这个公共方法 调用的所有方法 都必须是 staic { int y = 5; int z; Console.WriteLine("The first time,Y is {0}", y); Console.WriteLine("The first time,Z is {0}", "\"Z is unintitialized\""); //其实ref out可以形成重载函数 SquareRef(ref y); SquareOut(out z); Console.WriteLine(y);//25 Console.WriteLine(z);//36 Square(y); Square(z); Console.WriteLine(y);//25 没有ref,out不改变原值 Console.WriteLine(z);//36 } static void SquareRef(ref int x ) { x=x*x; } static void SquareOut(out int x) { x = 6; x = x * x; } static void Square(int x)//虽然这里是平方计算,但是 这个是void,无返回值,也无法改变原值 { x = x * x; } }
相关文章推荐
- FlexViewer学习资料,希望对初学者有帮助
- JAVA初学者之旅(希望对JAVA初学者学习有帮助)
- FlexViewer学习资料,希望对初学者有帮助
- FlexViewer学习资料,希望对初学者有帮助(转)
- 这是我在学习 second life 游戏时的学习与感受 希望对初学者有所帮助。
- Hibernate学习总结(非常详细的总结,希望对初学者有帮助)
- FlexViewer学习资料,希望对初学者有帮助(转)
- CCNA学习要点,希望能对初学者有帮助
- 通过H5的新标签canvas做出一个时钟的全过程,希望对初学者有帮助
- 我录制的部分.net视频(希望能给学习.net开发的初学者一些帮助)
- 通过QQ或者QQ帮助别人学习Lync汇总
- Python游戏编程入门(有目录及案例图片)-通过简单游戏学习python
- LXC学习实践(4)一个实际的案例
- 刚刚来到论坛,希望大家帮助,也希望可以和大家一起学习
- 【翠字营原创】基于 cryptlib.lib 开发的几种加密解密算法经典例子 涉及: MD5 AES AES-CBC 希望对大家学习有帮助!
- 实际案例:在现有代码中通过async/await实现并行
- 通过生活用例学习UDP协议并实际抓包分析
- 刚开始学习安卓SOlite出现问题希望有人帮助解决一下
- {传智播客} (学习笔记)--通过一些简单案例,了解Oracle存储过程编写和调用
- Java学习整理_写给自己的也希望能帮助需要的人_java入门基础