C#学习笔记之第一课
在这一课中,主要学习了如何用Visual Studio 2010创建项目编写一些简单的程序。
1.声明2个变量:int n1=10,n2=20;要求将两个变量交换,最后输出n1为20
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) main函数是程序的入口。
{ int n1 = 10, n2 = 20;//先声明后赋值再使用 int tmp = 0; tmp = n1; n1 = n2; n2 = tmp;
Console.WriteLine(n1);//向控制台输出一句话(字符串) Console.WriteLine(n2); Console.ReadKey();//让控制台先停留一下,等待用户输入.去掉这句话,也可以直接按ctrl+F5调试运行,控制台也不会一闪而过。 } } }
扩展(*):不适用第三个变量如何交换:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { int n1 = 10, n2 = 20;//先声明后赋值再使用 n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2; Console.WriteLine(n1);//向控制台输出一句话(字符串) Console.WriteLine(n2); Console.ReadKey(); } } }
2.用方法来实现;将上题封装一个方法来做,提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref.
方法的写法:
【访问修饰符】 方法返回值类型 方法名(参数列表){
//方法体
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { int n1=10,n2=20; Exchange(n1,n2); Console.WriteLine(n1);//向控制台输出一句话(字符串) Console.WriteLine(n2); Console.ReadKey(); } static void Exchange(int n1, int n2) { int tmp = 0; tmp = n1; n1 = n2; n2 = tmp; } } }
然后ctrl+F5后,可在控制台中查看到没有发生交换。
结果为:n1=10,n2=20
值参数 当利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并且将此拷贝传递给该方法。被调用的方法不会修改内存中实参的值,所以使用值参数时,可以保证实际值是安全的。在调用方法时,如果形式化参数的类型是值参数的话,调用的实参的值必须保证是正确的值表达式。程序员并没有实现他希望交换值的目的。
简单的来说,在main函数中内存区域有2个内存地址,存放n1=10,n2=20.当调用Exchange方法函数时,开辟出新的内存区域,会再次创建2个内存地址,且原有的值拷贝到方法中,方法中的n1=10,n2=20,然后交换方法中的n1与n2的值,即方法中的n1=20,n2=10.但是main中的n1与n2的值不变。所以如果在方法中再添加
Console.WriteLine(n1);//向控制台输出一句话(字符串) Console.WriteLine(n2); Console.ReadKey();
会查看到n1=20,n2=10.
那么如何实现变量交换呢?这里需要用到的是引用型参数。
引用型参数 和值参不同的是,引用型参数并不开辟新的内存区域。当利用引用型参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。
在方法中,引用型参数通常已经初始化。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { int n1=10,n2=20; Exchange(ref n1,ref n2); Console.WriteLine(n1);//向控制台输出一句话(字符串) Console.WriteLine(n2); Console.ReadKey(); } static void Exchange(ref int n1,ref int n2) { int tmp = 0; tmp = n1; n1 = n2; n2 = tmp; } } }
编译上述代码,程序将输出:n1=20,n2=10.
注意:在方法中使用引用型参数,会经常可能导致多个变量名指向同一处内存地址。
3.请用户输入一个字符串,计算字符串中的字符个数,并输出。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("请输入字符串:");//向控制台输出一句话(字符串) string str = Console.ReadLine();//向控制台输入一句话(字符串) int num = str.Length; Console.WriteLine("您输入的字符长度是:"+num); Console.ReadKey(); } } }
4.用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { int max=GetMax(4,6); Console.WriteLine("最大数:"+max); Console.ReadKey(); } static int GetMax(int n1,int n2) { int max=0; if(n1>n2){ max = n1; } else{ max = n2; } return max; } } }
求最大值调用了GetMax函数,该函数在判断最大值时有更简便的方法。即
static int GetMax(int n1,int n2) { return n1 > n2 ? n1 : n2; //n1>n2的话返回n1,否则n2.
} } }
得到的结果与先前一样,但是代码更简洁了。
5.用方法来实现:计算1-100之间的所有整数的和。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { int sum=GetSum(1,100); Console.WriteLine("和:"+sum); Console.ReadKey(); } static int GetSum(int start,int end) { int sum = 0; for(int i=start;i<=end;i++) { sum+=i; } return sum; } } }
6.计算1-100中所有奇数的和。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace HelloWorld 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 int sum=GetOdd(1,100); 13 Console.WriteLine("和:"+sum); 14 Console.ReadKey(); 15 } 16 17 static int GetOdd(int start,int end) 18 { 19 int sum = 0; 20 for(int i=start;i<=end;i++) 21 { 22 if(i%2!=0){//% 取余 /取整 奇数 23 sum += i; 24 } 25 } 26 return sum; 27 } 28 } 29 }
7.用方法来实现,判断一个给定的整数是否为质数。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace HelloWorld 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 bool result=GetPrime(100); 13 Console.WriteLine(result); 14 Console.ReadKey(); 15 } 16 static bool GetPrime(int n) 17 { 18 bool result=true; 19 if(n==1){ 20 return false; 21 22 } 23 for (int i = 2; i <= n / 2; i++)//i为除数 24 { 25 if (n % i == 0) 26 { 27 result=false; 28 break; 29 } 30 } 31 return result; 32 } 33 } 34 }
8.用方法来实现:计算1-100之间的所有质数(素数)的和。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace HelloWorld 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 int result = GetSum(1,100); 13 Console.WriteLine("1-100中所有奇数的和:"+result); 14 Console.ReadKey(); 15 } 16 static bool GetPrime(int n) 17 { 18 bool result=true; 19 if(n==1){ 20 return false; 21 22 } 23 for (int i = 2; i <= n / 2; i++)//i为除数 24 { 25 if (n%i == 0) 26 { 27 result=false; 28 break; 29 } 30 } 31 return result; 32 } 33 34 static int GetSum(int start, int end) 35 { 36 int sum = 0; 37 int i = 0; 38 for (i = start; i <= end; i++) 39 { 40 string js=GetPrime(i).ToString(); 41 42 if (js.Equals("True")) 43 { 44 Console.WriteLine("奇数:"+i); 45 sum = sum + i; 46 } 47
48 } 49 return sum; 50 } 51 52 } 53 }
9.用方法来实现:有一个整数数组:{1,3,5,7,90,2,4,6,8,10},找出其中最大值,并输出。不能调用数组的Max()方法。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 7 namespace HelloWorld 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] arr = { 1,3,5,7,96,2,4,6,8}; 14 int max=GetMax(arr); 15 Console.WriteLine(max); 16 Console.ReadKey(); 17 } 18 static int GetMax(int[] arr) 19 { 20 int max=arr[0]; 21 Console.WriteLine(arr.Length); 22 for (int i = 1; i < arr.Length; i++) 23 { 24 Console.WriteLine(arr[i]); 25 if (max<arr[i]) 26 { 27 max=arr[i]; 28 Console.WriteLine(max); 29 } 30 } 31 return max; 32 } 33 } 34 }
10.用方法来实现:有一个字符串数组:{”马龙“,”迈克尔乔丹“,”雷吉米勒“,”蒂姆邓肯“,”科比布莱恩特“},请输出最长的字符串。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 7 namespace HelloWorld 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] arr = {"马龙","迈克尔乔丹","雷吉米勒","蒂姆邓肯","科比布莱尔特"}; 14 string maxString=GetMax(arr); 15 Console.WriteLine(maxString); 16 Console.ReadKey(); 17 } 18 static string GetMax(string[] arr) 19 { 20 int j=0,max=arr[0].Length; 21 string maxString="none"; 22 for (int i = 1; i < arr.Length; i++)//找出这个数组中字符串长度最大值 23 { 24 if (max<arr[i].Length) 25 { 26 max = arr[i].Length; 27 } 28 } 29 30 for (j = 0; j < arr.Length; j++) 31 {//确定字符长度最大值的位置, 32 if (max == arr[j].Length) 33 { 34 35 maxString=arr[j]; 36 37 } 38 } 39 return maxString; 40 } 41 42 } 43 } 44 45

浙公网安备 33010602011771号