C# Day01
数据类型
/*
23,0,-15:整型int占4个字节
13.4:浮点数:单精度用float,双精度double,高精度decimal
3.5单精度后面加f占4字节
3.5默认双精度占8字节
3.5高精度数据后面加m占16字节
从精确度来看:decimal>double>float
从取值范围来看:doble>float>decimal
x,y属于字符类型,chair来表示,用单引号来表示字符,占2个字节
'x''y''o'","
zhangsan属于字符串,string来表示,其数据需要用""引起来
用双引号引起来的0个
真/假
变量和常量
//常量:程序在运行过程中不变的量.3,-5.3.5"zhangsan",'2'
//变量:程序在运行过程中可以改变的量.
//语法格式:类型名 变量名=值;
//int x = 23;
////flort,char,string,bool的变量,并分别赋值
//float f = 2.5f;
//char c = 'a';
//string s = "zhangsan";
//bool b = true;
//变量命名的规则:
//1.变量名里面只能包含数字(0-9),字母(a-z,A-Z)和下划线(_)
//2.只能字母和下划线开始
//3.同一个变量名不能够重复定义
//4.不能使用关键字定义
//5.区分大小写Int
//6.使用驼峰命名法(第一个单词全部小写,从第二个单词开始首字母大写),score,totalScore
//7.见名知意
//int age = 23;
算数运算符
//+用在字符串之间,其含义是两个字符串进行拼接
//Console.WriteLine("zhangsan"+"123");
//如果字符串和其他数据相加,先把其他数据转成字符串然后在和其他字符串进行拼接
Console.WriteLine(123+"zhangsan");
//除法:如果两边的数据都是整型,其结果也是整型,直接把整数部分舍掉,536/100
// Console.WriteLine(5 / 2);
//取余:%,536%100
// Console.WriteLine(5 % 2);
//练习题:9368通过代码,分别得到千位/百位/十位和各位上的数字
//int thousand = 9368 / 1000;
//int hundred = 9368 % 1000 / 100;
//int ten = 9368 % 100 / 10;
//int unit = 9368 % 10;
//int a = 10;
//a = a + 1;
//把变量的值自增1
// a++;
//++a;
//int num = a++;
//Console.WriteLine(num);
//Console.WriteLine(a);
//int count = ++a;
//++在前先++,++在后后++
//Console.WriteLine(count);
//Console.WriteLine(a);
//由变量或者常量和运算符组成的式子称为表达式,表达式都会有一个结果,该结果称为返回值
复合运算符
//int a = 10;
//int d=20;
//a = a + 1;
////复合运算符
//a += 1;
//a = a + d;
//a += d;
//语句:是程序运行的最小单位,通常是以;号为结束标志
基本输入和输出
////输出:Console.WriteLine(),Console.WriteLine,前者是打印自动换行,后者只打印不换行
////1.只输出一个数据
//int a = 10;
//Console.WriteLine(23);
//Console.WriteLine(a);
//Console.WriteLine(a + 23);
////2.输出两个数据
//Console.WriteLine(10 + "," + 23);
////3.格式化输出,即输出的数据可以带有一定的格式
//Console.WriteLine("{0},{1:p1},{2,-8:F3}", a, 0.23, 0.56);
//Console.WriteLine();
////输入:Console.Read()从键盘上读取数据,读取了字符的ASC码值
//int res = Console.Read();
//Console.WriteLine(res);
//res = Console.Read();
//Console.WriteLine(res);
//res = Console.Read();
//Console.WriteLine(res);
//// Console.ReadLine()读取从键盘上输入的字符串,以回车键作为结束标志,同时把回车键以后的字符从输入流里面清空
//string input = Console.ReadLine();
//Console.WriteLine(input);
数据类型转换
//把一个取值范围小精确度低的数据赋值给取值范围大精确度高的变量,此过程进行了隐式转换
//int a = 10;
//float num = a;
////强制类型转换
////1.数值之间的转换,使用强制类型转换符:(类型)
//a = (int)23.5f;
////2.把任何数据转成字符串
//string str = 123 + "";
//str = a.ToString();
////3.把字符串转成数值类型
//a = int.Parse("123");
//Console.WriteLine(a + 1);
////4.通用写法
//a = Convert.ToInt32(23.5f);
//str = Convert.ToString(123);
//a = Convert.ToInt32("123");
//1.从键盘上输出数据
//Console.WriteLine("请输入第一个数据:");
//string s1 = Console.ReadLine();
//Console.WriteLine("请输入第二个数据:");
//string s2 = Console.ReadLine();
////2.进行加减乘除运算
//float num1 = Convert.ToSingle(s1);
//float num2 = Convert.ToSingle(s2);
////3.把得到的结果输出出来
//Console.WriteLine("加:{0},减:{1},乘:{2},除:{3}", num1 + num2, num1 - num2, num1 * num2, num1 / num2);
//string str = Console.ReadLine();
int num = Convert.ToInt32(str);
//Console.WriteLine(num);
//Console.WriteLine("hello world");
try
{
//把有可能出现异常的代码放到try里面,如果代码出现异常,则运行eatch里面的代码,否则只能运行try里面的代码
string str = Console.ReadLine();
num = Convert.ToInt32(str);
Console.WriteLine(num);
}
catch (Exception ex)
{
//当try里面的代码有异常时,ex保存的是异常的信息
Console.WriteLine(ex);
Console.WriteLine(num);
}
//try和catch一定要同时存在,finally可有可无
finally
{
Console.WriteLine("hello world");
}
..................................................................................................................................................
C# Day02
比较运算符
//比较运算符组成的表达式,其结果是bool类型
//int a = 10;
//int b = 20;
//bool result = a == b;
//Console.WriteLine(result);
逻辑运算符
//逻辑与(&&),逻辑或(||),逻辑非(!)
//逻辑运算符两边的值均是bool类型
//int a = 10;
//int b = 30;
//int c = 30;
//当&&两边都是true的时候,整个表达的结果为true只要有一个结果是假,整个表达的结果就是false
//bool res = (a < b) && (a < c);
//当||两边的值都是false的时候,整个表达式的结果为false,只要有一个值为true,整个个表达的结果就是true
//res = (a < b) || (a > c++);
//Console.WriteLine(res);
//Console.WriteLine(c);
if语句
//语法格式:if(bool类型的表达式){语句}
// int a = 10;
//int b = 20;
//if (a<b)
{
// Console.WriteLine("hello world");
}
//109m,77M,Read读取的是字符的asc值
//int input = Console.Read();
//Console.WriteLine(input);
//Console.WriteLine((int)'m');
//string str = Console.ReadLine();
//if (input==(int)'m'||input==(int)'m')
// {
// Console.WriteLine("男性");
//}
//if (str=="m"||str=="M")
//{
// Console.WriteLine("男性");
//}
//if (str!="m"||str!="M")
//{
// Console.WriteLine("女性");
条件运算符
//int a = Convert.ToInt32(Console.ReadLine());
//int b = Convert.ToInt32(Console.ReadLine());
//int max = 0;
// if (a>b)
{
// max = a;
}
//else
{
//max = b;
}
//max=(a>b ? a:b)>c?(a.b ? a:b):c;
//string str =a > b ? "a>b":"a<b";
Switch...case
//Console.WriteLine("输入一个数字");
//int input = Console.Read();
//if (input == '1')
//{
// Console.WriteLine("春天");
//}
//else if (input=='2')
//{
// Console.WriteLine("夏天");
//}
//else if (input=='3')
//{
// Console.WriteLine("秋天");
//}
//else if (input=='4')
//{
// Console.WriteLine("冬天");
//}
//}
//}
//注意事项:
//1.如果case下面有代码,则一定要有break;
//2.当程序运行break之后,会直接跳出Switch后面的大括号,执行大括号后面的代码
//3当所有case后面的值
//4.case的位置可以不固定
//5.如果case下面没有代码,可以省去break,此时该case就和下面的case公用一段代码
int str = Convert.ToInt32(Console.ReadLine());
{ switch (str)
{
case 1:
Console.WriteLine("星期一爬山");
break;
case 2:
Console.WriteLine("星期二跑步");
break;
case 3:
Console.WriteLine("星期三学习");
break;
case 4:
Console.WriteLine("星期四打游戏");
break;
case 5:
Console.WriteLine("星期五看电影");
break;
case 6:
Console.WriteLine("星期六健身");
break;
case 7:
Console.WriteLine("星期天休息");
break;
..................................................................................................................................................
Day03
while循环
/语法格式:while(bool类型的表达式) {要重复运行的代码}
//循环变量初始化
// int num = 0;
//循环条件
// while (num<5)
{
//循环体
// Console.WriteLine("hello world");
//循环变量增量
// num++;
// Console.WriteLine(num);
break和continue
1.break在switch里面作用是跳出Switch...case在循环体里面其作用是跳出本层循环
2.如果只知道循环条件而不知道循环次数的情况下,通常使用while循环,同时会结合break来使用
3.continue提前结束本次循环,进入下一次循环,代码执行contioue之后,countiue后面的代码不再执行
do...while循环
while循环判断循环条件之后在进行循环,又称为当型型循环
//do...while循环:不管循环条件是否成立,先执行一次循环
for循环
for循环通常用在已知次数的情况
//变量的作用范围,又称为变量的作用域,在其作用域,不能够重复定义该变量,同时出了作用域,该变量就不能使用
嵌套的循环
循环体里面又一个循环,称为循环的嵌套
////外层循环控制打印的行数,内层循环控制打印的列数。
/for (int j = 1;j <= 9; j++)
//{
// for (int i = 1; i <= j; i++)
// {
// Console.Write("{0}*{1}={2,-3}", i,j,i*j);
// }
// Console.WriteLine();
一维数组
//如果处理的数据类型一样,通常使用数组
//根据数组所包含数据类型,可以分为整型数组,浮点型数组,字符数组,字符串数组,bool类型数组
//数组的声明:类型名[]数组名
//int a;
//int[] ages;
//float[] a;
//string[] b;
//数组的赋值称为初始化,共有三种方式的初始化
//第一种:动态初始化,new是关键字,用来给数组或者对象分配内存空间,用来保存数据,5是数组的长度,即数组所存放数据的个数,{}里面的 数据称为元素,
//ages = new int[5] { 1, 2, 3, 4, 5 };
//int[] ages1 = new int[] { 1, 2, 3, 4, 5, 6, };
//第二种:没有给数组元素赋具体的值,此时系统会给元素赋一个默认值,数值类型的数组,默认值为0,bool类型数组默认值为false,字符串数 组,默认值为空,字符数组默认值为空
//scores = new float[10];
//names = new string[5];
//第三种:静态初始化,数组的声明和初始化必须在一行
//int[] arr =[3, 5, 6, 8, 9, 2];
//数组作为一个整体不能够直接参与运算,但赋值除外
// Console.WriteLine(arr);
//要得到数组中的元素,需要使用数组名[下标]
//arr[0] = 4;
//元素下标的最大值是数组长度-1
// Console.WriteLine(arr[0]);
//for (int i = 0; i < ages.Length; i++)
{
//0,1,2,3,4
// sum = sum + i;
// Console.WriteLine(ages[i]);
}
..................................................................................................................................................
Day04
冒泡排序
//int[] a = { 6, 1, 2, 4, 5 };
//for (int i = 0;i < a.Length-1;i++)//i=0,1,2,3
//{
// for (int j = 0;j < a.Length-1-i;j++)//0
// {
// if (a[j]<a[j+1])
// {
// int t = a[j];
// a[j] = a[j + 1];
// a[j + 1] = t;
// }
// }
//}
//for (int i = 0;i < a.Length;i++)
//{
// Console.WriteLine(a[i]);
//}
字符串
//string str = "beijingzhangsan";
////字符串本质上来讲是一个字符数组,字符串的变量名,即为数组名
////但只能通过数组名[下标]读取某个字符,但是不能够修改某个元素的值,因为它是只读的
//int count = 0;
//for (int i = 0;i < str.Length;i++)
//{
// if (str[i]=='i')
// {
// count++;
// }
//}
//Console.WriteLine(count);
////str[0] = 'p';
二维数组
//int[] a = new int[] { 1, 2, 3, 4 };
////二维数组的定义
//int[,] arr = new int[2,3 ] { { 1, 2, 3 }, { 4, 5, 6 } };
//int[,] brr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
//int[,] crr = { { 1, 2, 3 }, { 4, 5, 6 } };
////Console.WriteLine(arr[1,0]);
//for (int i = 0;i < 2;i++)//0,1
//{
// for (int j = 0;j < 3;j++)//0,1,2
// {
// //a[0,0]a[0,1]a[0,2],a[1,0]a[1,1]a[1,2]
// Console.Write(arr[i,j]);
// }
// Console.WriteLine();
//}
//int[,] a = { { 1, 2, 3 }, { 4, 5, 6 } };
//int[,] b = new int[a.GetLength(1), a.GetLength(0)];
//for (int i = 0;i < b.GetLength(0);i++)
//{
// for (int j = 0;j < b.GetLength(1);j++)
// {
// b[i, j] = a[j, i];
// }
//}
//int[,] arr = { { 52, 24, 89 }, { 2, 46, 90 } };
//int max = arr[0, 0];
//int hang = 0;
//int lie = 0;
//for (int i = 0;i < arr.GetLength(0);i++)
//{
// for (int j = 0;j < arr.GetLength(1);j++)
// {
// if (max<arr[i,j])
// {
// max = a[i, j];
// hang = i;
// lie = j;
// }
// }
//}
//int[,] a = new int[4, 3];
//for (int i = 0;i < a.GetLength(0);i++)
//{
// for (int j = 0;j < a.GetLength(1);j++)
// {
// Console.WriteLine("请输入第{0}行,第{1}列:",i,j);
// a[i, j] = Convert.ToInt32(Console.ReadLine());
// }
//}
//for (int i = 0;i < a.GetLength(0);i++)
//{
// for (int j = 0;j < a.GetLength(1);j++)
// {
// Console.Write("{0,-4}", a[i, j]);
// }
// Console.WriteLine();
//}
//for (int j = 0;j < a.GetLength(1);j++)
//{
// Console.WriteLine(a[0,j]);
//}
//int sum = 0;
//for (int j = 0;j < a.GetLength(1);j++)
//{
// sum = sum + a[2, j];
//}
foreach
int sum = 0;
int ji = 1;
int[] a = { 1, 2, 3, 4, 5 };
foreach (var item in a)
{
sum = sum + item;
ji = ji * item;
Console.WriteLine(item);
}
int[,] b = { { 1, 2, 3 }, { 4, 5, 6 } };
foreach (var item in b)
{
Console.WriteLine(item);
}
string s = "hellooffcn";
int count = 0;
foreach (var item in s)
{
if (item=='l')
{
count++;
}
}
Console.WriteLine(count);
.............................................................................................................................................
day05
//枚举又叫一一列举,它是把我们要赋的值先列举出来,然后再通过选择的方式,得到我们要赋的值
//enum是关键字,用来定义一个枚举类型的,enum后面是类型名
enum Sex
{
男,女
}
//定义枚举类型:四季,生肖
enum Season
{
Spring=1,
Summer,
Autumn,
Winter
}
enum ShengXiao
{
鼠,
牛,
虎,
兔,
龙,
蛇,
马,
羊,
猴,
鸡,
狗,
猪
}
enum Fenji
{
市场部=800,
人力部=900
}
enum GameStatus
{
Pause,
Start,
Load,
Exit,
Running,
Resume
}
enum Act
{
Walk,
Run,
Jump,
Dead,
Sneak,
Shoot,
Throw,
}
enum Attack
{
Normal=2,
Magic=2,
Bao=80
}
//struct是关键字,用来定义一个结构体类型,关键字后面是类型名
//大括号里面定义的变量称为成员变量或者字段
//1.public是访问修饰符,用来控制结构体或者类里面的成员,在结构体或者类的外面是否能被访问到,如果没有public访问修饰符,该成员在类的外面不能够被访问
//2.要想访问结构体或者类里面的成员,需要使用点运算符
struct PersonInfo
{
public string name;
public Sex sex;
public int age;
public double height;
public string address;
}
struct Student
{
public string name;
public int age;
public string number;
public int score;
}
struct Birth
{
public int year;
public int month;
public int day;
}
struct Test
{
public string name;
public Sex sex;
public Birth birth;
public double[] scores;
}
class Program
{
static void Main(string[] args)
{
////枚举的类型名是开发者自定义的,故类型名每个单词的首字母必须大写,赋值时,使用枚举类型名加.运算符,选择要赋的值,点运算符是用来点出枚举/结构体或者类里面的成员的.其运算级别最高
//Sex s = Sex.男;
////1.每一个枚举值都会对应一个默认的整数,该整数从0开始,向下依次加1,故可以使用强制类型转换符在整数和枚举值之间进行转换
//s = (Sex)3;
//Console.WriteLine(s);
////2.可以给枚举值赋一新的整数值
////string str = Console.ReadLine();
////int a = Convert.ToInt32(str);
////Season season = (Season)a;
////switch (a)
////{
//// case 1:
//// Console.WriteLine("春天");
//// break;
//// case 2:
//// Console.WriteLine("夏天");
//// break;
//// case 3:
//// Console.WriteLine("秋天");
//// break;
//// case 4:
//// Console.WriteLine("冬天");
//// break;
////}
////switch (season)
////{
//// case Season.Spring:
//// Console.WriteLine("春天");
//// break;
//// case Season.Summer: