C#几个基础知识训练
显示三角(数清楚该输出几个空格,该输出几个星星就行,两个循环)
public static void Main()
{
Console.Write("请输入行数:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
for (int i = 1; i <= lines; i++)
{
for (int k = 1; k <= lines - i; k++)
Console.Write(" ");
for (int j = 1; j <= i * 2 -1; j++)
Console.Write("*");
Console.WriteLine("");
}
Console.ReadLine();
}
1+到100(编程之美)
public static void Main()
{
int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;
Console.WriteLine(sum);
Console.ReadLine();
}
递归!,阶乘
using System;
class Factor
{
public static void Main()
{
Console.Write("请输入数字:");
int num = int.Parse(Console.ReadLine());
Console.WriteLine("{0} 的阶乘是 {1}", num, Factorial(num)); Console.ReadKey();
}
public static long Factorial(long n)
{
if (n == 1)
return 1;
else
return n * Factorial(n - 1);
}
}
冒泡(两层循环,第一层依次过一遍,第二层互换)
using System;
class ArraySort
{
public static void Main()
{
int[] d = { 10, 15, 21, 43, 17, 98, 2, 74, 63, 10 };
int temp;
//冒泡法排序
for (int i = 0; i < d.Length; i++)
for (int j = i + 1; j < d.Length; j++)//这个循环下来,最大的留在了最左边
if (d[i] < d[j])//小的往上升
{
temp = d[i];
d[i] = d[j];
d[j] = temp;
}
//输出排序结果
foreach (int i in d)
Console.Write("{0}, ", i); Console.ReadKey();
}
}
找质数(数学问题,一个数除以它的平方根以下的--1上以的正整数,余数都不=0就是质数)
using System;
class Factor
{
public static void Main()
{
for (int i = 2; i < 100; i++)
if (IsPrame(i))
Console.Write(i + ","); Console.ReadKey();
}
public static bool IsPrame(int n)
{
for (int j = 2; j <= Math.Sqrt(n); j++)
if (n % j == 0)
return false;
return true;
}
}
99表(输出的应用)
using System;
public class JiuJiuBiao
{
public static void Main(string[] args)
{
int i,j;
for(i=1; i<10; i++)
{
for(j=1; j<10; j++)
{
Console.Write("{0:D1}*{1:D1}={2,2} ", i, j, i*j);
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
------------------------------------------------------------------------------------------
public static void Main()
{
int i = 0;
int j = 0;
for (int n = 1; n <= 9; n++)
{
for (int m = 1; m <= n; m++)
{
Console.Write("{0}*{1}={2} ",n,m,m*n);
}
Console.WriteLine("");
}
Console.ReadKey();
}
参数类型分为 in(实参值不变), ref(改变实参值), out(改变实参值) 三种,默认为 in
using System;
class MethodCall
{
public static void Main()
{
/*
* 参数类型分为 in, ref, out 三种,默认为 in。
* in 类型在子方法中修改了对应变量后,主方法中的值不会发生改变。
* ref 类型在子方法中修改了对应变量后,主方法中的值也会发生改变。
* out 主方法中对应的变量不需要初始化。
*/
int a = 3, b = 4, c;
Console.WriteLine("Before Method Call : a = {0}, b = {1}, c 未赋值", a, b);
AMethod(a, ref b, out c);
Console.WriteLine("After Method Call : a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadKey();
}
public static void AMethod(int x, ref int y, out int z)
{
x = 7;
y = 8;
z = 9;
}
}
方法重载:方法名相同,参数列表不同,根据传递的参数决定用哪个方法。
多态,看下面:
Code
using System;
class Car
{
public virtual void Drive()
{ Console.WriteLine("Car"); }
}
class Truck : Car
{
public override void Drive()
{ Console.WriteLine("Truck"); }
}
class Client
{
public static void Main()
{
Car c = new Truck();
c.Drive(); //多态性决定着将调用Truck的Drive方法
Console.ReadKey();
}
}
输出:Truck
构造函数(对象实例化的时候就调用构造函数!!new的时候)
using System;
public class Person
{
public string name = "";
public int age = 0;
//默认构造函数
public Person()
{
ShowInfo();
}
//构造函数重载(1)
public Person(int Age)
{
this.age = Age;
ShowInfo();
}
//构造函数重载(2)
public Person(int Age, string Name)
{
this.age = Age;
this.name = Name;
ShowInfo();
}
public void ShowInfo()
{
Console.WriteLine("The name is : " + name);
Console.WriteLine("The age is:" + age);
}
}
class Client
{
public static void Main()
{
Person p1 = new Person();
//p1.ShowInfo();
Console.WriteLine("==========================");
Person p2 = new Person(30);
// p2.ShowInfo();
Console.WriteLine("==========================");
Person p3 = new Person(30, "Tom");
//p3.ShowInfo();
Console.ReadKey();
}
}
类的字段,属性,(私有字段出了类就访问不了了,只能类内的共有属性或者方法访问了,属性可以写成只读,只写,可读可写)
using System;
class Account
{
private double balance = 0; //字段
public double Balance //属性
{
get { return balance; }
set { balance = value; }
}
/*=============================================================
* 我们可以通过修改get、set方法达到控制存取的目的。
* 例如:
*
* 1)只读属性
* public double Balance //属性
* {
* get { return balance; }
* set { }
* }
*
* 2)读写控制
* public double Balance
* {
* get
* {
* if(Console.ReadLine()=="1234")
* return balance;
* else
* return -9999999;
* }
* set { }
* }
* =============================================================
*/
public void Deposit(double n)
{ this.balance += n; }
public void WithDraw(double n)
{ this.balance -= n; }
}
class Client
{
public static void Main()
{
Account a = new Account();
a.Balance = 1000; // 可以读写属性,因为属性Balance是public型的
//a.balance = 1000; //不可以读写字段,因为字段balance是private型的
Console.WriteLine(a.Balance);
a.WithDraw(500); Console.WriteLine(a.Balance);
a.Deposit(2000);
Console.WriteLine(a.Balance); Console.ReadKey();
}
}
属性,方法的作用范围
(1、自身类可以访问自己任何访问权限的属性
2、子类可以访问父类的共有属性,保护属性,不能访问私有属性
3、client类中不能访问base类的保护属性和私有属性)
using System;
class Base
{
/*
* public 的可访问范围是所有类
* private 的可访问范围是当前类
* protected 的可访问范围是当前类及其子类
*/
public string name = "Tom";
private double salary = 1500;
protected int age = 20;
public virtual void ShowInfo()
{
Console.WriteLine(this.name); //可以,因为name是 public 型的
Console.WriteLine(this.salary); //可以,salary是private型,在Base类中可以访问
Console.WriteLine(this.age); //可以,因为age是protected型,在子类中可以访问
}
}
class Derived : Base
{
public override void ShowInfo()
{
Console.WriteLine(this.name); //可以,因为name是 public 型的
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就无法访问
Console.WriteLine(this.age); //可以,因为age是protected型,在子类中可以访问
}
}
class Client
{
public static void Main()
{
Base b = new Base();
Console.WriteLine(b.name); //可以,因为name是 public 型的
//Console.WriteLine(b.salary); //不可以!!!!!!!!!
//Console.WriteLine(b.age);//不可以!!!!!!!!!!
//Console.WriteLine(this.salary); //不可以,salary是private型,超出Base就无法访问
//Console.WriteLine(this.age); //不可以,因为age是protected型,Client不是Base的子类
Console.WriteLine("==========================");
b.ShowInfo();
Console.WriteLine("==========================");
Derived d = new Derived();
d.ShowInfo();
Console.ReadKey();
}
}
继承接口IComparable的person类排序
using System;
using System.Collections;
public class Person : IComparable
{
public int ID;
public string Rank;
public Person(int id, string rank)
{
this.ID = id; this.Rank = rank;
}
#region IComparable Members
/*
* IComparable 接口只有一个方法: CompareTo。CompareTo方法
* 只接收一个object类型的参数,这意味着它可以接收任何类
* 型的数据(object是所有类的父类),这个方法会返回一
* 整型数值,含义如下:
*
* 1) 小于零,当前实例(this)小于obj对象
* 2) 等于零,当前实例(this)等于obj对象
* 3) 大于零,当前实例(this)大于obj对象
*
* Int32,Int16,String,Decimal等数据类型都已经实现了IComparable接口
*/
public int CompareTo(object obj)
{
Person p = (Person)obj;
return this.ID.CompareTo(p.ID);
}
#endregion
}
class SortArrayList
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person(8, "排长"));
list.Add(new Person(5, "团长"));
list.Add(new Person(1, "司令"));
list.Add(new Person(4, "旅长"));
list.Add(new Person(7, "连长"));
list.Add(new Person(2, "军长"));
list.Add(new Person(6, "营长"));
list.Add(new Person(3, "师长"));
list.Sort();
Console.WriteLine("After Sorting");
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
} Console.ReadKey();
}
}
再麻烦一点:()
using System;
using System.Collections;
public enum enuSortOrder
{
IDAsc, IDDesc, RankAsc, RankDesc
}
public class Person : IComparable
{
public static enuSortOrder intSortOrder = enuSortOrder.IDAsc;
public int ID;
public string Rank;
public Person(int id, string rank)
{
this.ID = id;
this.Rank = rank;
}
#region IComparable Members
/*
* IComparable 接口只有一个方法: CompareTo。CompareTo方法
* 只接收一个object类型的参数,这意味着它可以接收任何类
* 型的数据(object是所有类的父类),这个方法会返回一
* 整型数值,含义如下:
*
* 1) 小于零,当前实例(this)小于obj对象
* 2) 等于零,当前实例(this)等于obj对象
* 3) 大于零,当前实例(this)大于obj对象
*
* Int32,Int16,String,Decimal等数据类型都已经实现了IComparable接口
*/
public int CompareTo(object obj)
{
Person p = (Person)obj;
switch ((int)intSortOrder)
{
case (int)enuSortOrder.IDAsc:
return this.ID.CompareTo(p.ID);
case (int)enuSortOrder.IDDesc:
return p.ID.CompareTo(this.ID);
case (int)enuSortOrder.RankAsc:
return RankCompare(this.Rank, p.Rank);
case (int)enuSortOrder.RankDesc:
return RankCompare(p.Rank, this.Rank);
default:
return this.ID.CompareTo(p.ID);
}
}
private int RankCompare(string rank1, string rank2)
{
int intRank1 = ConvertRankToInt(rank1);
int intRank2 = ConvertRankToInt(rank2);
if (intRank1 < intRank2)
return -1;
else if (intRank1 == intRank2)
return 0;
else
return 1;
}
private int ConvertRankToInt(string rank)
{
if (rank == "司令")
return 8;
else if (rank == "军长")
return 7;
else if (rank == "师长")
return 6;
else if (rank == "旅长")
return 5;
else if (rank == "团长")
return 4;
else if (rank == "营长")
return 3;
else if (rank == "连长")
return 2;
else
return 1;
}
#endregion
#region IComparable 成员
int IComparable.CompareTo(object obj)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
class SortArrayList
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person(8, "排长"));
list.Add(new Person(5, "团长"));
list.Add(new Person(1, "司令"));
list.Add(new Person(4, "旅长"));
list.Add(new Person(7, "连长"));
list.Add(new Person(2, "军长"));
list.Add(new Person(6, "营长"));
list.Add(new Person(3, "师长"));
list.Sort();
Console.WriteLine("Sort By ID Asc:");
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By ID Desc:");
Person.intSortOrder = enuSortOrder.IDDesc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By Rank Asc:");
Person.intSortOrder = enuSortOrder.RankAsc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.WriteLine("----------------------------");
Console.WriteLine("Sort By Rank Desc:");
Person.intSortOrder = enuSortOrder.RankDesc;
list.Sort();
foreach (Person person in list)
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
Console.ReadKey();
}
}