using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _002_变量
{classProgram{staticvoidMain(string[] args){int age =20;//声明变量int hp =60;string name ="pr";
Console.WriteLine(name);
Console.ReadKey();}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _004_练习_定义变量储存主角的信息
{classProgram{staticvoidMain(string[] args){string name ="李丹兰";int hp =100;int level =34;float exp =345.3f;
Console.WriteLine("主角的名字是:\n\"{0}\"\n 血量是:\"{1}\"\n 等级是:\"{2}\"\n 经验是:\"{3}\"",name,hp,level,exp);
Console.ReadKey();}}}
005-在字符串前面加上字符@
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _005_在字符串前面加上字符
{classProgram{staticvoidMain(string[] args){string str1 ="I'm a good man,\n but you are a bad girl.";string str2 =@"I'm a good man. \n you are a bad girl. ";
Console.WriteLine(str1);
Console.WriteLine(str2);//@的左右//1.加@使不识别字符串中的转义字符,除了双引号(使用两个引号表示一个引号)//2.允许字符串定义在多行//string str3 = "I'm a good man,// \n but you are a bad girl.";string str3 =@"I'm a good man,
\n but you are a bad girl.";
Console.WriteLine(str3);//3.在表示路径时string path1 ="c:\\csharp\\study";string path2 =@"c:\csharp\study";
Console.WriteLine(path1);
Console.WriteLine(path2);//path1 与 path2 输入结果是一致的。
Console.ReadKey();}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _034_数组的遍历
{classProgram{staticvoidMain(string[] args){//第一种遍历方式int[] scores ={1,2,3,4,4,5,5,5,5};for(int i =0; i < scores.Length; i++){
Console.WriteLine(scores[i]);}//第二种遍历方式int id=0;while(id<scores.Length){
Console.WriteLine(scores[id++]);}第三种遍历方式foreach(int temp in scores){
Console.WriteLine(temp);}
Console.ReadKey();}}}
035-字符串的处理
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _035_字符串的处理
{classProgram{staticvoidMain(string[] args){string str =" www.taikr.Com ";for(int i =0; i < str.Length; i++){
Console.WriteLine(str[i]);}string res = str.ToLower();//变小写,返回副本,对原字符串没有影响string res1 = str.Trim();//移除首尾空格字符,对原字符串没有影响string res2 = str.TrimEnd();string res3 = str.TrimStart();
Console.WriteLine(res);
Console.WriteLine(res1+"|");
Console.WriteLine(res2+"|");
Console.WriteLine(res3+"|");string[] strArray = str.Split('.');foreach(string temp in strArray){
Console.WriteLine(temp);}
Console.ReadKey();}}}
036-函数的定义
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _036_函数的定义
{classProgram{staticvoidWrite(){//这里是函数体也是方法体,这里可以写0行,也可多行
Console.WriteLine("Text output from function");}staticintadd(int a,int b){return a + b;}staticvoidMain(string[] args){Write();
Console.WriteLine(add(23,323));
Console.ReadKey();}}}
037-函数的定义和使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _037_函数的定义和使用
{classProgram{staticint[]GetDivisor(int num){int count =0;for(int i =1; i <= num; i++){if(num % i ==0){
count++;}}int[] array =newint[count];int index =0;for(int j =1; j <= num; j++){if(num % j ==0)
array[index++]= j;}return array;}staticvoidMain(string[] args){int[] array =GetDivisor(9);foreach(int temp in array){
Console.WriteLine(temp);}
Console.ReadKey();}}}
038-参数数组-定义一个参数个数不确定的函数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _038_参数数组_定义一个参数个数不确定的函数
{classProgram{staticintPlus(paramsint[] array)//这里定义了一个int类型参数数组{int res =0;int n = array.Length;for(int i =0; i < n; i++){
res += array[i];}return res;}staticvoidMain(string[] args){int res =Plus(1,3,3,4,4,4,4,23);//参数数组就是帮我们减少了一个创建数组的过程
Console.WriteLine(res);
Console.ReadKey();}}}
039-结构函数的定义和使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _039_结构函数的定义和使用
{struct CustomerName
{publicstring firstName;publicstring lastName;publicstringGetName(){return firstName +" "+ lastName;}}struct Vector3
{publicfloat x;publicfloat y;publicfloat z;publicfloatDistance(){return(float)Math.Sqrt(x * x + y * y + z * z);}}classProgram{staticvoidMain(string[] args){CustomerName myName;
myName.firstName ="PENG";
myName.lastName ="RUI";
Console.WriteLine("My name is "+myName.GetName());
Console.ReadKey();}}}
040-函数的重载
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _040_函数的重载
{classProgram{//函数的重载是指函数名相同时,通过参数类型、数量不同,来实现不同的功能staticintMaxValue(paramsint[] array){int maxValue = array[0];for(int i =1; i < array.Length; i++){if(array[i]> maxValue){
maxValue = array[i];}}return maxValue;}staticdoubleMaxValue(paramsdouble[] array){double maxValue = array[0];for(int i =1; i < array.Length; i++){if(array[i]> maxValue){
maxValue = array[i];}}return maxValue;}staticvoidMain(string[] args){int res =MaxValue(232,4,4,5,100,3,3);//编译器会根据你传递过来的实参的类型去判定调用哪一个函数double res1 =MaxValue(2.4,22,3443.9,22);
Console.WriteLine(res);
Console.WriteLine(res1);
Console.ReadKey();}}}