C#入门
- Lesson1-C#文件结构
- Lesson2-变量
- Lesson3-变量的本质
- Lesson4-变量的命名规则
- Lesson5-常量
- Lesson6-转义字符
- Lesson7-类型转换之隐式转换
- Lesson8-类型转换之显示转换
- Lesson9-异常捕获
- Lesson10-算数运算符
- Lesson11-字符串拼接
- Lesson12-条件运算符
- Lesson13-逻辑运算符
- Lesson14-位运算符
- Lesson15-三目运算符
- Lesson16-条件分支语句if
- Lesson17-条件分支语句switch
- Lesson18-循环语句while
- Lesson19-循环语句do while
- Lesson20-循环语句for
- Lesson21-C#中的随机数
Lesson1-C#文件结构
C#拥有三种注释类型
- 单行注释 //书写注释内容//
- 多行注释 /*书写注释内容*/
- 函数注释 ///书写注释内容
C#的文件结构
- using System 代表引用命名空间
可以理解为引用了一个工具包 - namespace 代表一个命名空间
命名空间可以理解为一个工具包 - class 代表一个类
类可以理解为工具包中的工具 - Main 代表一个函数(方法)
Main函数是程序的主入口,入门教程的代码都会写在Main函数中
using System;
namespace Lesson1_第一个应用程序
{
class Program
{
static void Main(string[] args)
{
}
}
}
接下来我们可以写出我们的第一句代码
using System;
namespace Lesson1_第一个应用程序
{
class Program
{
static void Main(string[] args)
{
//在控制台打印一行信息 打印信息结束后自动空一行
//如果是双引号之间的内容 对于符号没有特别要求
Console.WriteLine("你好世界!");
Console.WriteLine("Hello World");
//在控制台打印信息 但是打印完成后不会自动空行
Console.Write("你好,我喜欢你");
Console.Write("我要好好学习,天天向上");
//检测玩家输入的代码
//等待玩家输入完毕后(按回车键)才会继续执行后面的代码
//玩家可以输入 很多信息 知道回车结束
Console.WriteLine("请玩家输入");
Console.ReadLine();
Console.WriteLine("玩家输入完毕");
//检测玩家是否按键 只要按了键盘上的任意键 就会认为输入结束
Console.ReadKey();
Console.WriteLine("玩家输入完毕");
//输入 向控制台输入内容
Console.ReadLine();
Console.ReadKey();
//输出 在控制台打印信息
Console.WriteLine("123");
Console.Write("123");
}
}
}
Lesson2-变量
讲解变量之前我们先了解什么是代码折叠
- 主要作用 是让我们编程时 逻辑更加清晰
- 它是有 #region #endregion配对出现的
- 它的具体作用是可以将中间包裹的代码折叠起来 避免代码太凌乱
- 本质是 编辑器提供给我们的 预处理指令 它只会在编辑时有用 发布了代码 或执行代码它会被自动删除
using System;
namespace Lesson2_变量
{
class Program
{
static void Main(string[] args)
{
//函数语句块 目前学习的内容 都写在这里面
Console.WriteLine("变量")
#region 知识点二 如何申明变量
//变量的含义:
// 可以变化的容器 变量就是用来存储各种不同类型数值的一个容器
// 不同的变量类型可以存储不同类型的值
//变量申明固定写法:
// 变量类型 变量名 = 初始值;
int one = 1;
//变量类型 有14种变化
//变量名我们自定义 但是要按照一定规则
//初始值一定要和变量类型是统一
//= 和 ; 是固定不变的
//变量类型
// 一定要死记硬背 各种变量类型的关键字
// 一定要记忆 各种不同变量类型 所能存储的范围
// 一定要记住 各种不同变量类型 所能存储的类型
//1.有符号的整形变量
//是能存储一定范围正负数包括0的变量类型
//sbyte -128~127
sbyte sb = 1;
//潜在知识点 通过+来进行拼接打印
Console.WriteLine("sbyte变量sb中存储的值是:" + sb);
//int -21亿~21亿多
int i = 2;
//short -32768~32767之间的数
short s = 3;
//long -9百万兆~9百万兆之间的数
long l = 4;
//2.无符号的整形变量
//是能存储一定范围0和正数的变量类型
//byte 0~255
byte b = 1;
//uint 0~42亿多的一个范围
uint ui = 2;
//ushort 0~65535之间的一个数
ushort us = 3;
//ulong 0~18百万兆之间的数
ulong ul = 4;
//3.浮点数(小数)
//float 存储7/8位有效数字 根据编译器不同 有效数字也可能不一样
//它会四舍五入
//有效数字 是从左到右 第一个非0数字开始计算的 例如0.00123456f 实际上是从1开始算的
//之所以要在后面加f 是因为c#中 申明的小数 默认是double的类型 加f 是告诉系统 它是float类型
float f = 1.01234567890f;
Console.WriteLine(f);
//double 存储15~17位有效数字 抛弃的数字 会四舍五入
double d = 0.12345678901234567890123456789;
Console.WriteLine(d);
//decimal 存储27~28位的有效数字 不建议使用
decimal de = 0.123456789012345678901234567890m;
Console.WriteLine(de);
//4.特殊类型
//bool true false 表示真假的数据类型 真假类型
bool bo = true;
bool bo2 = false;
Console.WriteLine(bo + "_" + bo2);
//char 是用来存储单个字符的变量类型 字符类型
char c = '唐';
Console.WriteLine(c);
//string 是字符串类型 用来存储多个字符的 没有上限
string str = "的骄傲了肯定就发生123123sdafjkasdkfjaskldjfAKKSAJD";
Console.WriteLine(str);
int x = 1000;
Console.WriteLine(x);
//变量的使用和修改 不能不中生有 必须要先声明才能用
x = 900;
Console.WriteLine(x);
#endregion
#region 知识点三 为什么有那么多不同的变量类型
//不同的变量存储的范围和类型不一样 本质是占用的内存空间不同
//选择不同的数据(变量)类型装载不同的数据
//姓名
string myName = "唐老狮";
//年龄
byte age = 18;
//身高
float height = 177.5f;
//体重
float weight = 68.5f;
//性别 true女 false男
bool sex = false;
//数字用int 小数用float 字符串用string 真假用bool
#endregion
#region 知识点四 多个相同类型变量 同时申明
int i2 = 1;
float f2 = 3.2f;
string str2 = "123";
bool bo3 = true;
//多个同类型变量申明
//固定写法
//变量类型 变量名 = 初始值,变量名 = 初始值,变量名 = 初始值...;
int a1 = 1, b1 = 2, c1 = 3, d1 = 4;
Console.WriteLine(b1);
string s1 = "123", s2 = "234";
Console.WriteLine(s1 + s2);
#endregion
#region 知识点五 变量初始化相关
//变量申明时 可以不设置初始值 但是不建议这样写 这样不安全
//a2是有值的 默认值为0
int a2;
a2 = 1;
Console.WriteLine(a2);
#endregion
}
}
}
Lesson3-变量的本质
using System;
namespace Lesson3_变量本质
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("变量本质");
#region 变量类型回顾
// 有符号
//sbyte (-128~127)
//int (-21亿多~21亿多)
//short (-3万多~3万多)
//long (-9百万兆多~9百万兆多)
// 无符号
//byte (0~255)
//uint (0~42亿多)
//ushort (0~6万多)
//ulong (0~18百万兆多)
// 浮点数
//float (7~8位有效数字)
//double (15~17位有效数字)
//decimal (27~28位有效数字)
// 特殊
//bool (true和false)
//char (一个字符)
//string (一串字符)
#endregion
#region 知识点一 变量的存储空间(内存中)
// 1byte = 8bit
// 1MB = 1024byte
// 1GB = 1024MB
// 1TB = 1024GB
// 通过sizeof方法 可以获取变量类型所占的内存空间(单位:字节)
//有符号
int sbyteSize = sizeof(sbyte);
Console.WriteLine("sbyte 所占的字节数为:" + sbyteSize);
int intSize = sizeof(int);
Console.WriteLine("int 所占的字节数为:" + intSize);
int shortSize = sizeof(short);
Console.WriteLine("short 所占的字节数为:" + shortSize);
int longSize = sizeof(long);
Console.WriteLine("long 所占的字节数为:" + longSize);
Console.WriteLine("******************************************");
//无符号
int byteSize = sizeof(byte);
Console.WriteLine("byte 所占的字节数为:" + byteSize);
int uintSize = sizeof(uint);
Console.WriteLine("uint 所占的字节数为:" + uintSize);
int ushortSize = sizeof(ushort);
Console.WriteLine("ushort 所占的字节数为:" + ushortSize);
int ulongSize = sizeof(ulong);
Console.WriteLine("ulong 所占的字节数为:" + ulongSize);
Console.WriteLine("******************************************");
//浮点数
int floatSize = sizeof(float);
Console.WriteLine("float 所占的字节数为:" + floatSize);
int doubleSize = sizeof(double);
Console.WriteLine("double 所占的字节数为:" + doubleSize);
int decimalSize = sizeof(decimal);
Console.WriteLine("decimal 所占的字节数为:" + decimalSize);
Console.WriteLine("******************************************");
//特殊类型
int boolSize = sizeof(bool);
Console.WriteLine("bool 所占的字节数为:" + boolSize);
int charSize = sizeof(char);
Console.WriteLine("char 所占的字节数为:" + charSize);
//sizeof是不能够得到string类型所占的内存大小的
//因为字符串长度是可变的 不定
//int stringSize = sizeof(string);
#endregion
#region 知识点二 变量的本质
//变量的本质是2进制——>计算机中所有数据的本质都是二进制 是一堆0和1
//为什么是2进制?
//数据传递只能通过电信号,只有开和关两种状态。所以就用0和1来表示这两种状态
//计算机中的存储单位最小为bit(位),他只能表示0和1两个数字
// 1bit 就是1个数 要不是0要不是1
// 为了方便数据表示
// 出现一个叫byte(字节)的单位,它是由8个bit组成的存储单位。
// 所以我们一般说一个字节为8位
// 1byte = 0000 0000
// 2进制和10进制的对比
// 2进制和10进制之间的相互转换
#endregion
}
}
}
Lesson4-变量的命名规则
using System;
namespace Lesson4_变量的命名规则
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("变量的命名规则");
#region 知识点一:必须遵守的规则
//1.不能重名
//2.不能以数字开头
//3.不能使用程序关键字命名
//4.不能有特殊符号(下划线除外)
//建议的命名规则:变量名要有含义——>用英文(拼音)表示变量的作用
//非常不建议的命名规则:用汉字命名
#endregion
#region 知识点二:常用命名规则
//驼峰命名法——首字母小写,之后单词首字母大写(变量)
string myName = "唐老狮";
string yourName = "你的名字";
string yourMotherName = "";
//帕斯卡命名法——所有单词首字母都大写(函数、类)
string MyName = "dskafj";
//潜在知识点——C#中对大小写是敏感的 是区分的
#endregion
}
}
}
Lesson5-常量
using System;
namespace Lesson5_常量
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("常量");
#region 知识点一 常量的申明
//关键字 const
//固定写法:
//const 变量类型 变量名 = 初始值;
//变量的申明
int i = 10;
//常量的申明
const int i2 = 20;
#endregion
#region 知识点二 常量的特点
//1.必须初始化
//2.不能被修改
//变量申明可以不初始化
string name;
//之后可以来修改
name = "123";
name = "345";
const string myName = "唐老狮";
//作用:申明一些常用不变的变量
//PI 3.1415926
const float PI = 3.1415926f;
Console.WriteLine(PI);
#endregion
}
}
}
Lesson6-转义字符
using System;
namespace Lesson6_转义字符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("转义字符");
#region 知识点一 转义字符的使用
//什么是转义字符?
//它是字符串的一部分 用来表示一些特殊含义的字符
//比如:在字符串中表现 单引号 引号 空行等等
//string str = "asld"fk";
#region 固定写法
//固定写法 \字符
//不同的 \和字符的组合 表示不同的含义
//常用转义字符
// 单引号 \'
string str = "\'哈哈哈\'";
Console.WriteLine(str);
// 双引号 \"
str = "\"哈哈哈\"";
Console.WriteLine(str);
// 换行 \n
str = "1231231\n23123123123";
Console.WriteLine(str);
// 斜杠 \\ 计算机文件路径 是要用到\符号的
str = "哈\\哈哈";
Console.WriteLine(str);
//不常用转义字符(了解)
// 制表符(空一个tab键) \t
str = "哈\t哈哈";
Console.WriteLine(str);
// 光标退格 \b
str = "123\b123";
Console.WriteLine(str);
// 空字符 \0
str = "1234\0123";
Console.WriteLine(str);
// 警报音 \a
str = "\a";
Console.WriteLine(str);
Console.WriteLine("1231231231\n123123213\a\t123123");
#endregion
#endregion
#region 知识点二 取消转义字符
string str2 = @"哈哈\哈哈";
Console.WriteLine(str2);
Console.WriteLine(@"\n\\");
#endregion
}
}
}
Lesson7-类型转换之隐式转换
using System;
namespace Lesson7_类型转换_隐式转换_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("类型转换——隐式转换");
// 什么是类型转换
// 类型转换 就是不同变量类型之间的相互转换
// 隐式转换的基本规则——>不同类型之间自动转换
// 大范围装小范围
#region 知识点一 相同大类型之间的转换
//有符号 long——>int——>short——>sbyte
long l = 1;
int i = 1;
short s = 1;
sbyte sb = 1;
//隐式转换 int隐式转换成了long
//可以用大范围 装小范围的 类型 (隐式转换)
l = i;
//不能够用小范围的类型去装在大范围的类型
//i = l;
l = i;
l = s;
l = sb;
i = s;
s = sb;
//无符号 ulong——>uint——>ushort——>byte
ulong ul = 1;
uint ui = 1;
ushort us = 1;
byte b = 1;
ul = ui;
ul = us;
ul = b;
ui = us;
ui = b;
us = b;
//浮点数 decimal double——>float
decimal de = 1.1m;
double d = 1.1;
float f = 1.1f;
//decimal这个类型 没有办法用隐式转换的形式 去存储 double和float
//de = d;
//de = f;
//float 是可以隐式转换成 double
d = f;
//特殊类型 bool char string
// 他们之间 不存在隐式转换
bool bo = true;
char c = 'A';
string str = "123123";
#endregion
#region 知识点二 不同大类型之间的转换
#region 无符号和有符号之间
//无符号 不能装负数的
byte b2 = 1; //0~255
ushort us2 = 1;
uint ui2 = 1;
ulong ul2 = 1;
//有符号
sbyte sb2 = 1;
short s2 = 1;
int i2 = 1;
long l2 = 1;
//无符号装有符号
// 有符号的变量 是不能够 隐式转换成 无符号的
//b2 = sb2;
//us2 = sb2;
//ul2 = sb2;
//有符号装无符号
// 有符号变量 是可以 装 无符号变量的 前提是 范围一定要是涵盖的 存在隐式转换
//i2 = ui2;//因为 有符号的变量 可能会超过 这个无符号变量的范围
i2 = b2;// 因为 有符号的变量 不管是多少 都在 无符号变量的范围内
#endregion
#region 浮点数和整数(有、无符号)之间
//浮点数装整数 整形转为浮点数 是存在隐式转换的
float f2 = 1.1f;
double d2 = 1.1;
decimal de2 = 1.1m;
//浮点数 是可以装载任何类型的 整数的
f2 = l2;
f2 = i2;
f2 = s2;
f2 = sb2;
f2 = ul2;
f2 = ui2;
f2 = us2;
f2 = b2;
f2 = 10000000000000000000;
Console.WriteLine(f2);
//decimal 不能隐式存储 float和double
//但是它可以隐式的存储整形
de = l2;
de = ul2;
// double ——> float ——> 所有整形(无符号、有符号)
// decimal ——> 所有整形(无符号、有符号)
//整数装浮点数 整数是不能隐式存储 浮点数 因为 整数 不能存小数
//i2 = f2;
#endregion
#region 特殊类型和其它类型之间
//bool bool没有办法和其它类型 相互隐式转换
bool bo2 = true;
char c2 = 'A';
string str2 = "1231";
//bo2 = i2;
//bo2 = ui2;
//bo2 = f2;
//i2 = bo2;
//ui2 = bo2;
//f2 = bo2;
//bo2 = c2;
//c2 = bo2;
//bo2 = str2;
//str2 = bo2;
//char char 没有办法隐式的存储 其它类型的变量
//c2 = i2;
//c2 = f2;
//c2 = ui2;
//c2 = str2;
//str2 = c2;
//char类型 可以隐式的转换成 整形和浮点型
//char隐式转换成 数值类型是
//对应的数字 其实是一个 ASCII码
// 计算机里面存储 2进制
// 字符 中文 英文 标点符号 在计算机中都是一个数字
// 一个字符 对应一个数字 ASCII码就是一种对应关系
i2 = c2;
Console.WriteLine(i2);
f2 = c2;
Console.WriteLine(f2);
ui2 = c2;
Console.WriteLine(ui2);
//str2 = c2;
//string 类型 无法和其它类型进行隐式转换
//i2 = str2;
//ui2 = str2;
//f2 = str2;
#endregion
#endregion
// 总结 隐式转换 规则
// 高精度(大范围)装低精度(小范围)
// double ——> float ——> 整数(无符号、有符号)——>char
// decimal ——> 整数(无符号、有符号)——>char
// string 和 bool 不参与隐式转换规则的
}
}
}
Lesson8-类型转换之显示转换
using System;
namespace Lesson8_类型装换_显示转换_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("类型装换——显示转换");
//显示转换——>手动处理 强制转换
#region 知识点一 括号强转
// 作用 一般情况下 将高精度的类型强制转换为低精度
// 语法: 变量类型 变量名 = (变量类型)变量;
// 注意: 精度问题 范围问题
//相同大类的整形
//有符号整形
sbyte sb = 1;
short s = 1;
int i = 40000;
long l = 1;
//括号强转 可能会出现范围问题 造成的异常
s = (short)i;
Console.WriteLine(s);
i = (int)l;
sb = (sbyte)s;
sb = (sbyte)i;
sb = (sbyte)l;
//无符号整形
byte b = 1;
uint ui = 1;
b = (byte)ui;
//浮点之间
float f = 1.1f;
double d = 1.1234567890123456789f;
f = (float)d;
Console.WriteLine(f);
//无符号和有符号
uint ui2 = 1;
int i2 = -1;
//在强转时 一定要注意范围 不然得到的结果 可能有异常
ui2 = (uint)i2;
Console.WriteLine(ui2);
i2 = (int)ui2;
//浮点和整形 浮点数强转成整形时 会直接抛弃掉小数点后面的小数
i2 = (int)1.64f;
Console.WriteLine(i2);
//char和数值类型
i2 = 'A';
char c = (char)i2;
Console.WriteLine(c);
//bool和string 是不能够通过 括号强转的
//bool bo = true;
//int i3 = (bool)bo;
//string str = "123";
//i3 = (int)str;
#endregion
#region 知识点二 Parse法
//作用 把字符串类型转换为对应的类型
//语法: 变量类型.Parse("字符串")
//注意: 字符串必须能够转换成对应类型 否则报错
//有符号
//string str2 = "123";
int i4 = int.Parse("123");
Console.WriteLine(i4);
// 我们填写字符串 必须是要能够转成对应类型的字符 如果不符合规则 会报错
//i4 = int.Parse("123.45");
//Console.WriteLine(i4);
// 值的范围 必须是能够被变量存储的值 否则报错
//short s3 = short.Parse("40000");
//Console.WriteLine(s3);
sbyte sb3 = sbyte.Parse("1");
Console.WriteLine(sb3);
//他们的意思是相同的
Console.WriteLine(sbyte.Parse("1"));
Console.WriteLine(long.Parse("123123"));
//无符号
Console.WriteLine(byte.Parse("1"));
Console.WriteLine(ushort.Parse("1"));
Console.WriteLine(ulong.Parse("1"));
Console.WriteLine(uint.Parse("1"));
//浮点数
float f3 = float.Parse("1.2323");
double d3 = double.Parse("1.2323");
//特殊类型
bool b5 = bool.Parse("true");
Console.WriteLine(b5);
char c2 = char.Parse("A");
Console.WriteLine(c2);
#endregion
#region 知识点三 Convert法
//作用 更准确的将 各个类型之间进行相互转换
//语法: Convert.To目标类型(变量或常量)
//注意: 填写的变量或常量必须正确 否则出错
//转字符串 如果是把字符串转对应类型 那字符串一定要合法合规
int a = Convert.ToInt32("12");
Console.WriteLine(a);
//精度更准确
// 精度比括号强转好一点 会四舍五入
a = Convert.ToInt32(1.45845f);
Console.WriteLine(a);
//特殊类型转换
//把bool类型也可以转成 数值类型 true对应1 false对应0
a = Convert.ToInt32(true);
Console.WriteLine(a);
a = Convert.ToInt32(false);
Console.WriteLine(a);
a = Convert.ToInt32('A');
Console.WriteLine(a);
//每一个类型都存在对应的 Convert中的方法
sbyte sb5 = Convert.ToSByte("1");
short s5 = Convert.ToInt16("1");
int i5 = Convert.ToInt32("1");
long l5 = Convert.ToInt64("1");
byte b6 = Convert.ToByte("1");
ushort us5 = Convert.ToUInt16("1");
uint ui5 = Convert.ToUInt32("1");
ulong ul5 = Convert.ToUInt64("1");
float f5 = Convert.ToSingle("13.2");
double d5 = Convert.ToDouble("13.2");
decimal de5 = Convert.ToDecimal("13.2");
bool bo5 = Convert.ToBoolean("true");
char c5 = Convert.ToChar("A");
string str5 = Convert.ToString(123123);
#endregion
#region 知识点四 其它类型转string
// 作用:拼接打印
// 语法:变量.toString();
string str6 = 1.ToString();
str6 = true.ToString();
str6 = 'A'.ToString();
str6 = 1.2f.ToString();
int aa = 1;
str6 = aa.ToString();
bool bo6 = true;
str6 = bo6.ToString();
//当我们进行字符串拼接时 就自动会调用 tostring 转成string
Console.WriteLine("123123" + 1 + true);
str6 = "123123" + 1 + true + 1.23;
#endregion
}
}
}
Lesson9-异常捕获
using System;
namespace Lesson9_异常捕获
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("异常捕获");
#region 作用
//将玩家输入的内容 存储 string类型的变量(容器)中
//string str = Console.ReadLine();
//Parse转字符串为 数值类型时 必须 要合法合规
//int i = int.Parse(str);
//通过对异常捕获的学习 可以避免当代码报错时 造成程序卡死的情况
#endregion
#region 基本语法
//必备部分
try
{
//希望进行异常捕获的代码块
//放到try中
//如果try中的代码 报错了 不会让程序卡死
}
catch(Exception e) when (e.InnerException != null)
{
//如果出错了 会执行 catch中的代码 来捕获异常
//catch(Exception e) 具体报错跟踪 通过e得到 具体的错误信息
}
//可选部分
finally
{
//最后执行的代码 不管有没有出错 都会执行其中的代码
//目前 大家可以不用写
}
//注意:异常捕获代码基本结构中 不需要加; 在里面去写代码逻辑时 每一句代码才加;
#endregion
#region 实践
try
{
string str = Console.ReadLine();
int i = int.Parse(str);
Console.WriteLine(i);
//.......
}
catch
{
Console.WriteLine("请输入合法数字");
}
//finally
//{
// Console.WriteLine("执行完毕");
//}
#endregion
}
}
}
Lesson10-算数运算符
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Lesson10_算数运算符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("算数运算符");
//算数运算符 是用于 数值类型变量计算的运算符
//它的返回结果是数值
#region 知识点一:赋值符号
// =
// 关键知识点 :
// 先看右侧 再看左侧 把右侧的值赋值给左侧的变量
string myName = "唐老狮";
int myAge = 18;
float myHeight = 177.5f;
#endregion
#region 知识点二: 算数运算符
#region 加 +
// 用自己计算 先算右侧结果 再赋值给左侧变量
int i = 1;
// 3
i = i + 2;
Console.WriteLine(i);
// 连续运算 先算右侧结果 再赋值给左侧变量
// 99
i = 1 + 3 + 89 + i + i;
Console.WriteLine(i);
//4
i = 1 + 2 + 1;
Console.WriteLine(i);
// 初始化时就运算 先算右侧结果 再赋值给左侧变量
int i2 = 1 + 2 + 4 + i;
Console.WriteLine(i2);
#endregion
#region 减 -
// 用自己计算 先算右侧结果 再赋值给左侧变量
int j = 1;
j = j - 1;
Console.WriteLine(j);
// 连续运算 先算右侧结果 再赋值给左侧变量
j = 1 - 2 - 3;
Console.WriteLine(j);
j = 1 - j;
Console.WriteLine(j);//5
// 初始化时就运算 先算右侧结果 再赋值给左侧变量
int j2 = 1 - j - 0;
Console.WriteLine(j2);
#endregion
#region 乘 *
// 用自己计算 先算右侧结果 再赋值给左侧变量
int c = 1;
c = c * 10;
Console.WriteLine(c);
// 连续运算 先算右侧结果 再赋值给左侧变量
c = 1 * 2 * 3;
Console.WriteLine(c);
c = 2 * c * 2;
Console.WriteLine(c);
// 初始化时就运算 先算右侧结果 再赋值给左侧变量
int c2 = c * 2;
Console.WriteLine(c2);
#endregion
#region 除 /
// 用自己计算 先算右侧结果 再赋值给左侧变量
int chu = 1;
chu = 10 / chu;
Console.WriteLine(chu);
// 连续运算 先算右侧结果 再赋值给左侧变量
// 初始化时就运算 先算右侧结果 再赋值给左侧变量
chu = 1;
chu = 1 / 2;
Console.WriteLine(chu);
//默认的整数 是int 如果用来做除法运算 要注意 会丢失小数点后的小数
//如果你想用浮点数来存储 一定是 在运算时要有浮点数的特征
float f = 1 / 2f;
Console.WriteLine(f);
#endregion
#region 取余 %
// 用自己计算 先算右侧结果 再赋值给左侧变量
int y = 4;
// 4 / 3 得到余数 1
y = y % 3;
Console.WriteLine(y);
// 连续运算 先算右侧结果 再赋值给左侧变量
y = 4 % 3 % 2;
Console.WriteLine(y);
// 初始化时就运算 先算右侧结果 再赋值给左侧变量
#endregion
#endregion
#region 知识点三:算数运算符的 优先级
//优先级 是指 在混合运算时的运算顺序
//乘除取余 优先级高于 加减 先算乘除取余 后算加减
// 1 + 3 + 1 + 6
int a = 1 + 2 * 3 / 2 + 1 + 2 * 3;
Console.WriteLine(a);
a = 1 + 4 % 2 * 3 / 2 + 1;
Console.WriteLine(a);
//括号可以改变优先级 优先计算括号内内容
a = 1 + 4 % (2 * 3 / 2) + 1;
Console.WriteLine(a);
//多组括号 先算最里层括号 依次往外算
a = 1 + (4 % (2 * (3 / 2))) + 1;
Console.WriteLine(a);
#endregion
#region 知识点四:算数运算符的 复合运算符
// 固定写法 运算符=
// += -= *= /= %=
//复合运算符 是用于 自己=自己进行运算
int i3 = 1;
i3 = i3 + 2;
Console.WriteLine(i3);
i3 = 1;
i3 += 2;//i3 = i3 + 2;
Console.WriteLine(i3);
i3 = 2;
i3 += 2;//4
i3 -= 2;//2
i3 /= 2;//1
i3 *= 2;//2
i3 %= 2;//0
Console.WriteLine(i3);
int i4 = 10;
// i4 += 4
i4 += 20 * 2 / 10;
Console.WriteLine(i4);
//注意:复合运算符 只能进行一种运算 不能混合运算
//i4 */-= 2;
#endregion
#region 知识点五:算术运算符的 自增减
int a2 = 1;
a2 = a2 + 1;
a2 = 1;
a2 += 1;
//自增运算符 让自己+1
a2 = 1;
a2++;//先用再加
Console.WriteLine(a2);
++a2;//先加再用
Console.WriteLine(a2);
a2 = 1;
Console.WriteLine(a2++);//1
//2
Console.WriteLine(++a2);//3
//自减运算符 让自己-1
a2 = 1;
a2--;//先用再减
--a2;//先减再用
a2 = 1;
Console.WriteLine(a2--);//1
//0
Console.WriteLine(--a2);//-1
#endregion
}
}
}
Lesson11-字符串拼接
using System;
namespace Lesson11_字符串拼接
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("字符串拼接");
#region 知识点一 字符串拼接方式1
//之前的算数运算符 只是用来数值类型变量进行数学运算的
//而 string 不存在算数运算符不能计算 但是可以通过+号来进行字符串拼接
string str = "123";
//用+号进行字符串拼接
str = str + "456";
Console.WriteLine(str);
str = str + 1;
Console.WriteLine(str);
// 复合运算符 +=
str = "123";
str += "1" + 4 + true;
Console.WriteLine(str);
str += 1 + 2 + 3 + 4;
Console.WriteLine(str);
str += "" + 1 + 2 + 3 + 4;
Console.WriteLine(str);
str = "";
str += 1 + 2 + "" + ( 3 + 4 );
Console.WriteLine(str);
str = "123";
str = str + (1 + 2 + 3);
Console.WriteLine(str);
//注意 : 用+号拼接 是用符号唯一方法 不能用-*/%....
#endregion
#region 知识点二 字符串拼接方式2
//固定语法
//string.Format("待拼接的内容", 内容1, 内容2,......);
//拼接内容中的固定规则
//想要被拼接的内容用占位符替代 {数字} 数字:0~n 依次往后
string str2 = string.Format("我是{0}, 我今年{1}岁, 我想要{2}", "唐老狮", 18, "天天学习,好好向上");
Console.WriteLine(str2);
str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false);
Console.WriteLine(str2);
#endregion
#region 控制台打印拼接
//后面的 内容 比占位符多 不会报错
//后面的 内容 比占位符少 会报错
Console.WriteLine("A{0}B{1}C{2}", 1, true, false,1,2);
Console.Write("A{0}B{1}C{2}", 1, true);
#endregion
}
}
}
Lesson12-条件运算符
using System;
namespace Lesson12_条件运算符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("条件运算符");
#region 知识点一 条件运算符
// 用于比较两个变量或常量
// 是否大于 >
// 是否小于 <
// 是否等于 ==
// 是否不等于 !=
// 是否大于等于 >=
// 是否小于等于 <=
// 条件运算符 一定存在左右两边的内容
// 左边内容 条件运算符 右边内容
int a = 5;
int b = 10;
//条件运算符 不能直接这样使用
//纯比较不用结果 那么对于我们来说 没有任何的意义
//a > b;
// 比较的结果 返回的是 一个 bool 类型的值
// true和false 如果比较的条件满足 那就返回true 不满足 就返回false
// 先算右边 再赋值给左边
bool result = a > b;
Console.WriteLine(result);
result = a < b;
Console.WriteLine(result);
result = a >= b;
Console.WriteLine(result);
result = a <= b;
Console.WriteLine(result);
result = a == b;
Console.WriteLine(result);
result = a != b;
Console.WriteLine(result);
#endregion
#region 知识点二 各种应用写法
//变量和变量比较
a = 5;
b = 10;
result = a < b;// true
//变量和数值(常量)比较
result = a < 10;// true
result = b > 5;// true
//数值和数值比较
result = 5 > 3;//true
result = 5 == 3; // false
result = 5 != 3; // true;
//计算结果比较
//条件运算符的 优先级 低于算数运算符
// 8 > 6
// 先计算 再比较
result = a + 3 > a - 2 + 3;// true
result = 3 + 3 < 5 - 1;//false
//左边 条件运算符 右边
#endregion
#region 知识点三 不能进行范围比较
a = 5;
//判断是否在某两个值之间
// 1 < a < 6
//在C#都不能这样写
//result = 1 < a < 6;
//要判断 一个变量是否在两个数之间 要结合 逻辑运算符的知识点
#endregion
#region 知识点四 不同类型之间的比较
//不同数值类型之间 可以随意进行条件运算符比较
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 222;
//只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
result = i > f;
result = f < d;
result = i > by;
result = f > ui;
result = ui > d;
//特殊类型 char string bool 只能同类型进行 == 和 != 比较
string str = "123";
char c = 'A';
bool bo = true;
result = str == "234";//false
result = str == "123";//true
result = str != "123";//false
result = c == 'B';//false
//不仅可以和自己类型进行 == != 还可以和数值类型进行比较
//还可以和 字符类型进行大小比较
result = c > 123;
result = c > 'B';
result = bo == true;//true;
#endregion
}
}
}
Lesson13-逻辑运算符
using System;
namespace Lesson13_逻辑运算符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("逻辑运算符");
//对bool类型 进行逻辑运算
#region 知识点一 逻辑与
//符号 && 并且
//规则 对两个bool值进行逻辑运算 有假则假 同真为真
bool result = true && false;
Console.WriteLine(result);
result = true && true;
Console.WriteLine(result);
result = false && true;
Console.WriteLine(result);
//bool相关的类型 bool变量 条件运算符
//逻辑运算符优先级 低于 条件运算符 算术运算
// true && true
result = 3 > 1 && 1 < 2;
Console.WriteLine(result);
int i = 3;
// 1 < i < 5;
// true && true
result = i > 1 && i < 5;
Console.WriteLine(result);
//多个逻辑与 组合运用
int i2 = 5;
// true && false && true && true
//在没有括号的情况下 从左到右 依次看即可
//有括号 先看括号内
result = i2 > 1 && i2 < 5 && i > 1 && i < 5;
Console.WriteLine(result);
#endregion
#region 知识点二 逻辑或
//符号 || 或者
//规则 对两个bool值进行逻辑运算 有真则真 同假为假
result = true || false;
Console.WriteLine(result);
result = true || true;
Console.WriteLine(result);
result = false || true;
Console.WriteLine(result);
result = false || false;
Console.WriteLine(result);
// false || true
result = 3 > 10 || 3 < 5;
Console.WriteLine(result);//true
int a = 5;
int b = 11;
// true || true || false
result = a > 1 || b < 20 || a > 5;
Console.WriteLine(result);
// ? && ?
// ? || ?
// ? 可以是写死的bool变量 或者 bool值
// 还可以是 条件运算符相关
#endregion
#region 知识点三 逻辑非
//符号 !
//规则 对一个bool值进行取反 真变假 假变真
result = !true;
Console.WriteLine(result);
result = !false;
Console.WriteLine(result);
result = !!true;
Console.WriteLine(result);
//逻辑非的 优先级 较高
result = !(3 > 2);
Console.WriteLine(result);
a = 5;
result = !(a > 5);
Console.WriteLine(result);
#endregion
#region 知识点四 混合使用优先级问题
// 规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
// 逻辑运算符优先级 低于 算数运算符 条件运算符(逻辑非除外)
bool gameOver = false;
int hp = 100;
bool isDead = false;
bool isMustOver = true;
//false || false && true || true;
//false || false || true;
result = gameOver || hp < 0 && !isDead || isMustOver;
Console.WriteLine(result);
#endregion
#region 知识点五 逻辑运算符短路规则
int i3 = 1;
// || 有真则真
// 只要 逻辑与或者逻辑或 左边满足了条件
// i3 > 0 true
// 只要 满足条件 右边的内容 对于我们来说 已经不重要
//逻辑或 有真则真 那左边只要为真了 右边就不重要
result = i3 > 0 || ++i3 >= 1;
Console.WriteLine(i3);
Console.WriteLine(result);
// false && i3 ++ > 1;抛弃后面不去计算
//逻辑与 有假则假 那左边只要为假了 右边就不重要
result = i3 < 0 && i3++ > 1;
Console.WriteLine(i3);
Console.WriteLine(result);
#endregion
}
}
}
Lesson14-位运算符
using System;
namespace Lesson14_位运算符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("位运算符");
//位运算符 主要用数值类型进行计算的
//将数值转换为2进制 再进行位运算
#region 知识点一 位与 &
// 规则 连接两个数值进行位计算 将数值转为2进制
// 对位运算 有0则0
int a = 1;// 001
int b = 5;// 101
// 001
//& 101
// 001 = 1
int c = a & b;
Console.WriteLine(c);
a = 3;// 011
b = 19;// 10011
// 00011
//& 10011
// 00011
c = a & b;//3
Console.WriteLine(c);
//多个数值进行位运算 没有括号时 从左到右 依次计算
a = 1;// 001
b = 5;// 101
c = 19;//10011
// 00001
//& 00101
// 00001
//& 10011
// 00001
int d = a & b & c;
Console.WriteLine(d);
a = 1;//001
b = 2;//010
Console.WriteLine(a & b);
#endregion
#region 知识点二 位或 |
// 规则 连接两个数值进行位计算 将数值转为2进制
// 对位运算 有1则1
a = 1;//001
b = 3;//011
c = a | b;
// 001
//| 011
// 011
Console.WriteLine(c);
a = 5; // 101
b = 10;// 1010
c = 20;//10100
// 00101
//| 01010
// 01111
//| 10100
// 11111 => 1 + 2 + 4 + 8 + 16 =31
Console.WriteLine(a | b | c);
#endregion
#region 知识点三 异或 ^
// 规则 连接两个数值进行位计算 将数值转为2进制
// 对位运算 相同为0 不同为1
a = 1; //001
b = 5; //101
// 001
//^101
// 100
c = a ^ b;
Console.WriteLine(c);
a = 10; // 1010
b = 11; // 1011
c = 4; // 100
// 1010
//^ 1011
// 0001
//^ 0100
// 0101 = 5
Console.WriteLine(a ^ b ^ c);
#endregion
#region 知识点四 位取反 ~
// 规则 写在数值前面 将数值转为2进制
// 对位运算 0变1 1变0
a = 5;
// 0000 0000 0000 0000 0000 0000 0000 0101
// 1111 1111 1111 1111 1111 1111 1111 1010
// 反码补码知识
c = ~a;
Console.WriteLine(c);
#endregion
#region 知识点五 左移<< 和 右移>>
// 规则 让一个数的2进制数进行左移和右移
// 左移几位 右侧加几个0
a = 5; // 101
c = a << 5;
// 1位 1010
// 2位 10100
// 3位 101000
// 4位 1010000
// 5位 10100000 = 32 + 128 = 160
Console.WriteLine(c);
// 右移几位 右侧去掉几个数
a = 5; // 101
c = a >> 2;
// 1位 10
// 2位 1
Console.WriteLine(c);
#endregion
}
}
}
Lesson15-三目运算符
using System;
namespace Lesson15_三目运算符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("三目运算符");
#region 知识点一 基本语法
//套路: 3个空位 2个符号!!!
//固定语法:空位 ? 空位 : 空位;
//关键信息:bool类型 ? bool类型为真返回内容 : bool类型为假返回内容;
//三目运算符 会有返回值,这个返回值类型必须一致,并且必须使用!
#endregion
#region 知识点二 具体使用
string str = false ? "条件为真" : "条件为假";
Console.WriteLine(str);
int a = 5;
str = a < 1 ? "a大于1" : "a不满条件";
Console.WriteLine(str);
int i = a > 1 ? 123 : 234;
//第一个空位 始终是结果为bool类型的表达式 bool变量 条件表达式 逻辑运算符表达式
//第二三个空位 什么表达式都可以 只要保证他们的结果类型是一致的
bool b = a > 1 ? a > 6 : !false;
#endregion
}
}
}
Lesson16-条件分支语句if
using System;
namespace Lesson16_条件分支语句_if
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("条件分支语句");
#region 知识点一 作用
//让顺序执行的代码 产生分支
//if语句是第一个 可以让我们的程序 产生逻辑变化的 语句
#endregion
#region 知识点二 if语句
//作用: 满足条件时 多执行一些代码
//语法:
// if( bool类型值 ) // bool类型相关:bool变量 条件运算符表达式 逻辑运算符表达式
// {
// 满足条件要执行的代码 写在if代码块中;
// }
// 注意:
// 1.if语句的语法部分, 不需要写分号
// 2.if语句可以嵌套使用
if( false )
{
Console.WriteLine("进入了if语句代码块,执行其中的代码逻辑");
Console.WriteLine("进入了if语句代码块,执行其中的代码逻辑");
Console.WriteLine("进入了if语句代码块,执行其中的代码逻辑");
}
Console.WriteLine("if语句外的代码");
int a = 1;
if( a > 0 && a < 5)
{
Console.WriteLine("a在0到5之间");
}
string name = "唐老狮";
string passWord = "666";
if( name == "唐老狮" && passWord == "666" )
{
Console.WriteLine("登录成功");
}
//嵌套使用
if( name == "唐老狮" )
{
Console.WriteLine("用户名验证成功");
if( passWord == "666" )
{
Console.WriteLine("密码验证成功");
//可以无限嵌套
}
//可以无限嵌套
}
#endregion
#region 知识点三 if...else语句
// 作用:产生两条分支 十字路 满足条件做什么 不满足条件做什么
//语法:
// if( bool类型值 )
// {
// 满足条件执行的代码;
// }
// else
// {
// 不满足条件执行的代码:
// }
// 注意:
// 1.if ...else 语句 语法部分 不需要写分号
// 2.if ...else 语句 可以嵌套
if( false )
{
Console.WriteLine("满足if条件 做什么");
if( true )
{
if (true)
{
}
else
{
}
}
else
{
if (true)
{
}
else
{
}
}
}
else
{
Console.WriteLine("不满足if条件 做什么");
if (true)
{
}
else
{
}
}
//其它的使用和if的使用时一样
// 嵌套使用 也是和if语句 一样的
#endregion
#region 知识点四 if...else if...else 语句
//作用:产生n条分支 多条道路选择 最先满足其中的一个条件 就做什么
// 语法:
// if( bool类型值 )
// {
// 满足条件执行的代码;
// }
// else if( bool类型值 )
// {
// 满足条件执行的代码;
// }
// ...中间可以有n个 else if语句代码块
// else
// {
// 不满足条件执行的代码:
// }
// 注意:
// 1. 和前面两个是一样的 不需要写分号
// 2. 是可以嵌套的
// 3. else 是可以省略的
// 4. 注意 条件判断 从上到下执行 满足了第一个后 之后的都不会执行了
int a3 = 6;
if (a3 >= 10)
{
Console.WriteLine("a大于等于10");
}
else if( a3 > 5 && a3 < 10 )
{
Console.WriteLine("a在6和9之间");
}
else if( a3 >= 0 && a3 <= 5 )
{
Console.WriteLine("a在0和5之间");
}
else
{
Console.WriteLine("a小于0");
}
//if语句相关 if if..else if...else if...else
// else if 和 else 是组合套餐 根据实际情况选择使用
#endregion
}
}
}
Lesson17-条件分支语句switch
using System;
namespace Lesson17_条件分支语句_switch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("switch语句");
#region 知识点一 作用
//让顺序执行的代码 产生分支
#endregion
#region 知识点二 基本语法
//switch(变量)
//{
// // 变量 == 常量 执行 case和 break之间的代码
// case 常量:
// 满足条件执行的代码逻辑;
// break;
// case 常量:
// 满足条件执行的代码逻辑;
// break;
// case 可以有无数个
// default:
// 如果上面case的条件都不满足 就会执行 default中的代码
// break;
//}
// 注意: 常量!! 只能写一个值 不能去写一个范围 不能写条件运算符啊 逻辑运算符啊
// switch 只判断变量是否等于某一个固定值!!!!
int a = 3;
int a2 = 3;
switch (a)
{
//这个条件一定是常量
case 1:
Console.WriteLine("a等于1");
break;
case 2:
Console.WriteLine("a等于2");
break;
case 3:
Console.WriteLine("a等于3");
break;
default:
Console.WriteLine("什么条件都不满足,执行default中的内容");
break;
}
float f = 1.4f;
//它一般是配合枚举使用
switch (f)
{
case 1.5f:
Console.WriteLine("f等于1.5");
break;
case 1:
Console.WriteLine("f等于1.5");
break;
default:
Console.WriteLine("f什么条件都不满足,执行default中的内容");
break;
}
#endregion
#region 知识点三 default可省略
string str = "123";
switch (str)
{
case "123":
Console.WriteLine("等于123");
break;
case "234":
Console.WriteLine("等于234");
break;
}
#endregion
#region 知识点四 可自定义常量
char c = 'A';
//1.必须初始化 2.不能修改
const char c2 = 'A';
switch (c)
{
case c2:
Console.WriteLine("c等于A");
break;
default:
break;
}
#endregion
#region 知识点五 贯穿
//作用:满足某些条件时 做的事情是一样的 就可以使用贯穿
int aa = 1;
switch (aa)
{
// 不写case后面配对的break 就叫做贯穿
// 满足 1 3 4 2其中一个条件 就会执行 之后的代码
case 1:
case 3:
case 4:
case 2:
// case和break之间可以写n句语句
// 并且可以嵌套使用
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
Console.WriteLine("是个数字");
if( aa == 1 )
{
switch(aa)
{
default:
break;
}
}
else
{
}
break;
default:
break;
}
#endregion
}
}
}
Lesson18-循环语句while
using System;
namespace Lesson18_循环语句_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("while语句");
#region 知识点一 作用
// 让顺序执行的代码 可以不停的循环执行某一代码块的内容
// 条件分支语句 是 让代码产生分支
// 循环语句 是 让代码可以被重复执行
#endregion
#region 知识点二 语法相关
// bool类型变量 条件运算符 逻辑运算符
//while(bool类型的值)
//{
// //当满足条件时 就会执行while语句块中的内容
// //......
// //......
// //......
// //......
// //当代码逻辑执行完 会回到while循环开头
// //再次进行条件判断
//}
//Console.WriteLine("主代码逻辑");
//死循环
//就不停的执行循环中的逻辑 "直到死为止"
//死循环只有在目前我们学习 控制台程序时 会频繁使用
//之后进入 Unity过后 基本不会使用死循环
//1.可能因为内存问题 造成程序崩溃 闪退
//2.造成程序卡死
//while(true)
//{
// Console.WriteLine("**************");
// Console.WriteLine("请玩家输入用户名密码");
// Console.ReadLine();
//}
//计算一个为0的整形变量 让他只能累加1 不停的加到10为止
//int i = 0;
bool类型的值 还可以用逻辑运算符 && || ! 条件运算符 算术运算结合运算
//while(i < 10)
//{
// ++i;
//}
//Console.WriteLine(i);
#endregion
#region 知识点三 嵌套使用
// 不仅可以嵌套 if switch 还可以嵌套 while
//int a = 0;
//int b = 0;
//while(a < 10)
//{
// ++a;
// while( b < 10)
// {
// ++b;
// }
//}
//a = 0;
//b = 0;
//while( a < 10)
//{
// ++a;
// if( b < 10 )
// {
// ++b;
// }
//}
//int a2 = 0;
//while( a2 < 10 )
//{
// ++a2;
// //每次从外层循环进来是
// //b2和上一次的b2有没有关系 是不是一个变量
// //切忌 没有关系
// int b2 = 0;
// ++b2;
//}
#endregion
#region 知识点四 流程控制关键词
//作用 : 控制循环逻辑的关键词
// break:跳出循环
while(true)
{
Console.WriteLine("break之前的代码");
break;
Console.WriteLine("break之后的代码");
}
Console.WriteLine("循环外的代码");
int i = 0;
while(true)
{
++i;
Console.WriteLine(i);
if ( i == 10 )
{
break;
}
}
Console.WriteLine(i);
// continue:回到循环开始 继续执行
//while (true)
//{
// Console.WriteLine("continue前的代码");
// continue;
// Console.WriteLine("continue后的代码");
//}
//Console.WriteLine("循环外的代码");
//打印1到20之间的 奇数
int index = 0;
while(index < 20)
{
++index;
//什么样的数是奇数
//不能被2整除的数 ——> %
if (index % 2 == 0)
{
continue;
}
Console.WriteLine(index);
}
//注意:break和continue主要是和循环配合使用的 和if语句没关
// break在switch中的作用 和 while循环中的作用有异曲同工之妙
//while(true)
//{
// int a = 1;
// switch(a)
// {
// default:
// continue;
// break;
// }
// Console.WriteLine("11111");
//}
#endregion
}
}
}
Lesson19-循环语句do while
using System;
namespace Lesson19_循环语句_do_while
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("do...while循环");
#region 知识点一 基本语法
// while循环 是先判断条件再执行
// do while循环 是先斩后奏 先至少执行一次循环语句块中的逻辑 再判断是否继续
//do
//{
// //do while 循环语句块;
//} while (bool类型的值);
// 注意 do while 语句 存在一个重要的分号
#endregion
#region 知识点二 实际使用
// do while 使用较多
//do
//{
// Console.WriteLine("do while 循环语句块");
//} while (true);
//int a = 0;
//do
//{
// Console.WriteLine(a);
// ++a;
//} while (a < 2);
#endregion
#region 知识点三 嵌套使用
// if switch while do while
do
{
//if(true)
//{
//}
//while(true)
//{
//}
//int i = 1;
//switch (i)
//{
// default:
// break;
//}
//break;
//Console.WriteLine("111");
//continue;
//Console.WriteLine("111");
} while (false);
#endregion
}
}
}
Lesson20-循环语句for
using System;
namespace Lesson20_循环语句_for
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("for循环");
#region 知识点一 基本语法
//for( /*初始表达式*/; /*条件表达式*/; /*增量表达式*/ )
//{
// //循环代码逻辑;
//}
// 第一个空(初始表达式): 一般声明一个临时变量,用来计数用
// 第二个空(条件表达式): 表明进入循环的条件 一个bool类型的结果(bool变量 条件运算符 逻辑运算符 算术运算符)
// 第三个空(增量表达式): 用第一个空中的变量 进行 自增减运算
// 第一次进入循环时 才会调用 第一个空中的代码
// 每次进入循环之前 都会判断第二个空中的条件 满足才会进入循环逻辑
for( int i = 0; i < 10; i++ )
{
Console.WriteLine(i);
//执行完循环语句块中的逻辑后
//最后执行第三个空中的代码
}
for( int i = 10; i >= 0; i-- )
{
Console.WriteLine(i);
}
//每个空位 可以按照规则进行书写
//第一个空位 就是申明变量 所以可以连续申明
//第二个空位 就是进入条件 只要是bool结果的表达式 都可以
//第三个空位 就是执行一次循环逻辑过后要做的事情 做啥都行
//for( int i = 0, j = 0; i < 10 && j < 0; ++i, j = j + 1)
//{
//}
#endregion
#region 知识点二 支持嵌套
//for( int i = 0; i < 10; i++ )
//{
// for( int j = 0; j < 10; j++)
// {
// Console.WriteLine(i + "_" + j);
// }
// while(true)
// {
// }
// if(true)
// {
// }
// do
// {
// } while (true);
//}
#endregion
#region 知识点三 特殊写法
//for循环 这三个空位 可以都空着 可以根据需求去填写
// for循环可以写死循环
//for( ; ; )
//{
// Console.WriteLine("for循环的死循环");
//}
//int k = 0;
//for(; k < 10; )
//{
// ++k;//k++, k += 1;
//}
//for( k = 0; ; ++k )
//{
// if( k >= 10 )
// {
// break;
// }
//}
#endregion
#region 知识点四 对比while循环
//for循环 一般用来可以准确得到 一个范围中的所有数
for( int i = 0; i < 10; ++i )
{
}
int j = 0;
while(j < 10)
{
//......................
++j;
}
#endregion
}
}
}
Lesson21-C#中的随机数
namespace Lesson21_随机数
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Random random = new Random();
int i = random.Next();//随机生成一个非负数 可能会很大
i = random.Next(100);//随机生成一个0-99的数
i = random.Next(5,100);//随机生成一个5到99的数
}
}
}