C#入门
变量
变量存储数值的容器,是一门程序语言的最基础的部分。
不同的变量类型可以存储不同类型的数值。
种类:
在C#种一共有14种变量:
- 有符号类型4种
- 无符号类型4种
- 浮点数3种
- 特殊类型(char bool string)
//变量
//有符号类型的 范围 字节大小 位容量
sbyte sb=1; //-128~127 1byte 2^8
int i=2; //-21亿~21亿 4byte 2^(8*4)
short s=3; //-32768~32767 2byte 2^(8*2)
long l=4; //-9百万兆~9百万兆 8byte 2^(8*8)
//无符号类型 存储范围0~2^(n*8)-1 n为字节数
byte b=1;
uint ui=2;
ushort us=3;
ulong ul=4;
//浮点数 4byte
float f=1.01234567890f;//存储7~8位有效数字 根据编译器不同也有不同 四舍五入
//要加f 或F 默认存储类型位double 所以浮点数要加f表示为float类型存储
//double 存储15~17位有效数字 抛弃的数字 会四舍五入 8byte
double d = 0.12345678901234567890123456789;
//decimal 存储27~28位的有效数字 不建议使用 16byte 用的少
decimal de = 0.123456789012345678901234567890m; //尾部添加 m或者M 16byte
//特殊类型
bool bo=true; //1byte
char c='T'; //2byte
string="欢迎访问畅知的博客!";//引用类型 不固定大小
//=============================
//多个同类型变量的同时声明
int a1=1,a2=2,a3=5;
string s1="Hello",s2="TonyChang";
变量的命名规则
- 不能以数字开头
- 不能使用程序关键字命名
- 不能有除下划线之外的特殊符号
- 不能重名
常见的命名规则:
-
驼峰命名法 首字母首字符小写 其余字母首字符均大写(多用于变量)
string myName="畅知"; -
帕斯卡命名法 所有单词首字符都大写(多用于函数、类的命名)
class MyClassmate{}
常量
//常量的声明
const int i2=50;
其特点是必须初始化、不能被修改;
常量多用来存储一些常见的数值,例如Π,g等数学、物理定理性质的数据
在游戏开发中常用来表示固定的数值,玩家最大血量等
转义字符
转义字符是字符串的一部分,用来表示一些特殊含义的字符 比如:在字符串中表现 单引号 引号 空行等等
固定写法: \字符;
常见的:' " \n \ \t \0 \a;
此外还有取消转义字符:string ss=@“这个字符中的转义字符失效,原样子打印!”;
变量类型的转化:
转换原则 同类型的大的可以装小的,小类型的装大的就需要强制转换。
隐式转换:
同种类型的转换:
//有符号 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 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";
//特殊类型 bool char string
// 他们之间 不存在隐式转换
不同类型的转换:
char类型可以隐式转换成数值型,根据对应的ASCII码来进行转换。
无符号的无法隐式存储有符号的,而有符号的可以存储无符号的。
显示转换
-
括号强转(注意精度问题 范围问题)
//有符号类型 int i=1; short s=(short)i; //无符号类型 byte b=1; uint ui=(uint)b; //浮点数 float f=1.5f; double d=1.5; f=(float)d; //无符号和有符号 //要保证正数 注意范围 int ui2=1; int i2=1; ui2=(uint)i2; //浮点和整型 i2=(int)1.25f; //char和数值类型 i2='A'; char c=(char)i2; -
Parse方法
//Parse转换 int i4=int.Parse("123"); float f4=float.Parse("12.3"); //注意类型和范围! -
Convert法
int a=Convert.ToInt32("12"); a=Convert.ToInt32("1.35f");//会四舍五入 a=Convert.ToInt32(true);//转为1 false转为0注意:在Convert转换中变量以Int做标准,例如 INT16 为int,ToSingle为float
ToDouble为double,ToBoolean为bool;
-
其它类型转换为string(调用ToString方式)
string str=true.ToString();string str2=1.5f.ToString();
异常捕获
使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生。
try{}catch{}finally{}结构
//异常捕获
try
{
string str=Console.ReadLine();
int i=int.Parse(str);
Console.WriteLine("输入的字符串数值转为int的数值"+i);
}catch
{
Console.WriteLine("请输入合法数字");
}finally
{
//无论正常执行还是是否进行异常捕获 都会执行
Console.WriteLine("执行完毕");
}
运算符
算术运算符
算术运算符是英语数值类型变量运算的运算符,运算结果仍旧为数值。
赋值运算符:=
注意:赋值运算符理解将右边数值赋值给左边变量。
算术运算符:
//算术数运算符
//加+
int i1=5;
int sum=1+2+3+i1*2;
//减 -
int j1=6;
int sub=15-j1-2;
//乘 *
int c=5;
int c1=5*c;
//除 /
int d=28;
int d1=d/4;
//取模 (取余) %
int e=12;
e=e%5;
Console.WriteLine(e);
算术运算符的优先级
乘除取余 > 加减
int a = 1 + 2 * 3 / 2 + 1 + 2 * 3; //=11
Console.WriteLine(a);
a = 1 + 4 % 2 * 3 / 2 + 1; //2
Console.WriteLine(a);
//括号可以改变优先级 优先计算括号内内容
a = 1 + 4 % (2 * 3 / 2) + 1; //3
Console.WriteLine(a);
//多组括号 先算最里层括号 依次往外算
a = 1 + (4 % (2 * (3 / 2))) + 1; //2
Console.WriteLine(a);
复合运算符
+= -= *= /= %= 相当于对自身进行某项算术操作之后的结果重新赋给自己
//符合运算符
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;
自增/减运算符
注意理解前置还是后置的运算符,区别先用后自增/减还是先自增/减再使用。
可以理解为电击小子打怪物,小光先变身成电击小子打怪兽还是打完怪兽再变身。
//自增运算符
//自增运算符 让自己+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
//思考?这个
int a = 10, b = 20;
// 11 + 20
int number1 = ++a + b;
Console.WriteLine(number1);//31
a = 10;
b = 20;
//10 + 20
int number2 = a + b++;
Console.WriteLine(number2);//30
a = 10;
b = 20;
//10 + 21 + 11
int number3 = a++ + ++b + a++;
Console.WriteLine(number3);//42
Console.WriteLine(a);//12
字符串的拼接
-
使用+拼接
string str = "123"; //用+号进行字符串拼接 str = str + "456"; Console.WriteLine(str); str = str + 1; Console.WriteLine(str); //使用+= str = "123"; str += "1" + 4 + true; Console.WriteLine(str); //注意:只要遇到字符串,就是转为字符串 string str = "畅知"; str += 1 + 2 + 3 + 4; Console.WriteLine(str);//畅知10 str = ""; str += "" + 1 + 2 + 3 + 4;//开头就变为字符串 Console.WriteLine(str);//1234 str = ""; str += 1 + 2 + "" + (3 + 4);//先算括号,从首到尾计算 Console.WriteLine(str);//37 str = "123"; str = str + (1 + 2 + 3); Console.WriteLine(str);//1236 -
使用占位符替换方式拼接(占位符从0开始,用{}括起来)
string str2 = string.Format("我是{0}, 我今年{1}岁, 爱好:{2}", "畅知", 21, "我爱写博客!!"); Console.WriteLine(str2); str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false); Console.WriteLine(str2);
条件运算符
条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。
条件运算符的优先级要低于算术运算符
//条件运算符
int a = 5;
int b = 10;
//大于
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);
//也可以直接和数值比较
result=10>5;
//优先级
// 先计算 再比较
result = a + 3 > a - 2 + 3;// true
result = 3 + 3 < 5 - 1;//false
//不同类型之间的比较
//不同数值类型之间 可以随意进行条件运算符比较
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666;
//只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666;
bool result;
//只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
result = i > f;//true
Console.WriteLine(result);
result = f < d;//true
Console.WriteLine(result);
result = i > by;//false
Console.WriteLine(result);
result = f > ui;//false
Console.WriteLine(result);
result = ui > d;//true
Console.WriteLine(result);
//特殊类型 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
//不仅可以和自己类型进行 == != 还可以和数值类型进行比较
//字符参与比较大小时候将自身作为ASCII码比较
//还可以和字符类型进行大小比较
result = c > 123;
result = c > 'B';
result = bo == true;//true;
逻辑运算符
逻辑与 & 逻辑或 || 逻辑非 !
逻辑运算符优先级 < 条件运算符 算术运算
逻辑运算符中: !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
//逻辑运算符
//逻辑与&& 并且
//规则: 对两个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);
//符号 || 或者
//规则 对两个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值
// 还可以是 条件运算符相关
//----------逻辑非!
//符号 !
//规则 对一个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);
//混合使用逻辑运算符的优先级问题
// 规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
// 逻辑运算符优先级 低于 算数运算符 条件运算符(逻辑非除外)
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);
逻辑运算符的短路原则(聪明的运算符)
//短路原则
//|| 判断原则为有真则真 所以第一个条件判断为真,则直接可以得出运算结果为真 便不会检查第二个
//个运算条件的真假
//同理,&& 若左边条件为假,则不会判断右边条件,直接可以得出运算结果为假
//逻辑或 有真则真 那左边只要为真了 右边就不重要
int i3=1;
bool result = i3 > 0 || ++i3 >= 1;
Console.WriteLine(i3);//1
Console.WriteLine(result);
// false && i3 ++ > 1;抛弃后面不去计算
//逻辑与 有假则假 那左边只要为假了 右边就不重要
result = i3 < 0 && i3++ > 1;
Console.WriteLine(i3);//1
Console.WriteLine(result);
//思考?
//求打印结果是什么?
//注意运算符的优先级
bool gameOver;
bool isWin;
int health = 100;
gameOver = true;
isWin = false;
// true || false && true
Console.Write(gameOver || isWin && health > 0);
位运算符
位运算是基于二进制编码的运算,首先将值转换为二进制数值,然后对于位进行操作运算。
运算符:位与& 位或| 异或^ 位或 | 位取反! 左移<< 右移>>
//位运算符
//位与& 有0则0 全1才1
// 对位运算 有0则0
int a = 1;// 001
int b = 5;// 101
// 001
//& 101
// 001 = 1
int c = a & b;
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);
//位或| 有1则1,全0则0
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);
//异或^ =======
//不同为1 相同为0
// 对位运算 相同为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);
//位取反 ~ 取反
// 对位运算 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);
//左移 右移
// 规则 让一个数的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);
//练习----
//99 ^ 33 和 76 | 85 的结果为?
// 1100011 ^ 100001
// 1100011
//^0100001
// 1000010
Console.WriteLine(99 ^ 33);
// 1001100 | 1010101
// 1001100
//|1010101
// 1011101 => 64 + 29 = 93
Console.WriteLine(76 | 85);
三目运算符
条件?A :B 条件为真则走A逻辑否则走B
//三目运算符
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;
逻辑语句
条件分支语句
条件分支语句可以让顺序执行的代码逻辑产生分支,满足对应条件地执行对应代码逻辑。
IF语句
//IF语句块
int a=5;
if(a>0&&a<15)//注意结尾无分号
{
Console.WriteLine("a在0到15之间");
}
//if……else结构
if( false )
{
Console.WriteLine("满足if条件 做什么");
if( true )
{
if (true)
{
}
else
{
}
}
else
{
if (true)
{
}
else
{
}
}
}
else
{
Console.WriteLine("不满足if条件 做什么");
if (true)
{
}
else
{
}
}
//if……elseif 结构
int a3 = 6;
if (a3 >= 10)
{
Console.WriteLine("a3大于等于10");
}
else if( a3 > 5 && a3 < 10 )
{
Console.WriteLine("a3在6和9之间");
}
else if( a3 >= 0 && a3 <= 5 )
{
Console.WriteLine("a3在0和5之间");
}
else
{
Console.WriteLine("a小于0");
}
//对于初学者而言,代码逻辑要整齐,错落有致,方便对比嵌套逻辑语句块的配对
if语句的小练习--分辨奇偶数字
try
{
console.writeline("请输入一个整数");
int num = int.parse(console.readline());
//能被2整除的数 叫偶数
if (num % 2 == 0)
{
console.writeline("your input is even");
}
else
{
console.writeline("your input is odd");
}
}
catch
{
console.writeline("请输入数字");
}
语句块的知识
{}括起来的逻辑语句是一个代码块,注意变量在代码块中的生命周期
//语句块体悟
//语句块引起的变量的生命周期
//语句块中声明的变量只能在当前的语句块中使用
//体会当下代码在编译器中的报错意义!
int a = 1;
int b = 2;
{
int b = 3;
Console.WriteLine(a);
Console.WriteLine(b);
}
Console.WriteLine(b);
int a = 5;
if (a > 3)
{
int b = 0;
++b;
b += a;
}
Console.WriteLine(b);
Switch 语句
当判断条件过多时候,使用if elseif 来进行判断时,需要写多条elseif,显得冗长繁琐,为此体现出switch分支语句的优势
//switch语句
int a=2;
switch(a)
{
//这个条件一定是常量
case 1:
Console.WriteLine("a等于1");
break;//每个条件之间通过break隔开
case 2:
Console.WriteLine("a等于2");
break;
case 3:
Console.WriteLine("a等于3");
break;
default://可省略 默认选择条件
Console.WriteLine("什么条件都不满足,执行default中的内容");
break;
}
string str = "123";
switch (str)
{
case "123":
Console.WriteLine("等于123");
break;
case "234":
Console.WriteLine("等于234");
break;
}
//贯穿使用
//当一个变量同时满足多个条件可以做多条件的“合并”判断
//给变量对号找家--如果找到相关的可以接受的便会直接匹配,
//否则会继续匹配下一条case条件
string name="畅知";
switch (name)
{
//只要是符合三个条件之一就行
case "畅知":
case "TonyChang":
case "小明":
Console.WriteLine("是个帅哥!");
break;//break有阻断作用
case "小玉":
case "莉莉":
Console.WriteLine("是个美女!");
break;
default:
break;
}
switch使用练习:学生成绩的分档
//输入学生的考试成绩,如果
//成绩 >= 90:A
//90 > 成绩 >= 80:B
//80 > 成绩 >= 70:C
//70 > 成绩 >= 60:D
//成绩 < 60:E
//(使用switch语法完成)
//最后输出学生的考试等级
try
{
Console.WriteLine("请输入学生成绩");
int cj = int.Parse(Console.ReadLine());
// 取它的 十位数
// 100 / 10 = 10
// 99 / 10 = 9
// 84 / 10 = 8
// 74 / 10 = 7
// cj = cj / 10;
cj /= 10;
switch (cj)
{
case 10:
case 9:
Console.WriteLine("你的成绩是A");
break;
case 8:
Console.WriteLine("你的成绩是B");
break;
case 7:
Console.WriteLine("你的成绩是C");
break;
case 6:
Console.WriteLine("你的成绩是D");
break;
default:
Console.WriteLine("你的成绩是E");
break;
}
}
catch
{
Console.WriteLine("请输入数字");
}
循环语句
循环可以使满足循环执行条件的逻辑反复执行。注意不要随便写出死循环。
while循环
//while循环
int a=1;
while(a<10)//循环条件
{
++a;
}
Console.WriteLine(i);
//循环的嵌套使用
int a1=1;
int b=0;
while (a1 < 10)
{
++a1;
while (b < 10)
{
++b;
}
}
//break的使用
//break可以是执行逻辑点跳出while语句块
while (true)
{
Console.WriteLine("break之前的代码");
break;
Console.WriteLine("break之后的代码");
}
Console.WriteLine("循环外的代码");
//continue的使用
//使执行逻辑点跳出当前的循环内容
//直接进入下一次的循环判断执行
//打印1到20之间的 奇数
int index = 0;
while(index < 20)
{
++index;
//什么样的数是奇数
//不能被2整除的数 ——> %
if (index % 2 == 0)
{
continue;//跳过偶数情况
}
Console.WriteLine(index);
}
练习--找出100内所有素数打印
//找出100内所有素数并打印。
int num = 2;
while( num < 100 )
{
// 用想要判断是素数的数 从2开始 去取余 如果 中途就整除了 证明不是素数
// 如果 累加到和自己一样的数了 证明是素数
int i = 2;
while( i < num )
{
//判断是否整除
if( num % i == 0 )
{
break;
}
++i;
}
if( i == num )
{
Console.WriteLine(num);
}
++num;
}
doWhile循环
do……while语句与while循环差不多,只不过这个家伙太鲁莽,先斩后奏,不管如可,先执行代码块,再进行条件判断
//do while循环简单应用
string userName = "";
string passWord = "";
bool isShow = false;
do
{
//这句代码 第一次 肯定不能执行
if (isShow)
{
Console.WriteLine("用户名或密码错误,请重新输入");
}
//循环输入
Console.WriteLine("请输入用户名");
userName = Console.ReadLine();
Console.WriteLine("请输入密码");
passWord = Console.ReadLine();
isShow = true;
} while (userName != "畅知" || passWord != "666");
for循环
for循环是最常使用的一种循环语句,
//for循环
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)
{
}
//for循环的特殊使用
// for循环可以写死循环
//for( ; ; )
//{
// Console.WriteLine("for循环的死循环");
//}
int k = 0;
for(; k < 10; )
{
++k;//k++, k += 1;
}
//for( k = 0; ; ++k )
//{
// if( k >= 10 )
// {
// break;
// }
//}
for循环的经典练习:
一般都是找要执行逻辑块执行结果和循环条件变量之间的对应关系
//在控制台上输出如下10 * 10的空心星型方阵
//**********
//* *
//* *
//* *
//* *
//* *
//* *
//* *
//* *
//**********
//行
for (int j = 0; j < 10; j++)
{
//列
for (int i = 0; i < 10; i++)
{
//列 如果是 第1行和最后1行 那么 内层列循环 都打印星号
// 按照 **********的规则打印
if (j == 0 || j == 9)
{
Console.Write("*");
}
//否则 就是 按照* *的规则打印
else
{
if (i == 0 || i == 9)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
}
Console.WriteLine();
}
//在控制台上输出如下10行的三角形方阵
// * 1 1 -> 2i - 1 9 10 - i
// *** 2 3 -> 2i - 1 8 10 - i
// ***** 3 5 7 10 - i
// ******* 4 7 6 10 - i
// ********* 5 9 5
// *********** 6 11 4
// ************* 7 13 3
// *************** 8 15 2
// ***************** 9 17 1
//******************* 10 19 0 10 - i
//行
for (int i = 1; i <= 10; i++)
{
//打印空格的列
for (int k = 1; k <= 10 - i; k++)
{
Console.Write(" ");
}
//打印星号的列
for (int j = 1; j <= 2*i-1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
//在控制台上输出九九乘法表
for (int i = 1; i <= 9; i++)
{
//1 1 X 1 = 1 空行
//2 1 X 2 = 2 2 X 2 = 4 空行
//3 1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 空行
for (int j = 1; j <= i; j++)
{
Console.Write("{0}X{1}={2} ", j, i, i * j);
}
Console.WriteLine();
}
//求1~100之间所有偶数的和
int sum = 0;
for (int i = 1; i <= 100; i++)
{
//判断是否是偶数 是否能整除2
if( i % 2 == 0 )
{
sum += i;
}
}
for (int i = 2; i <= 100; i += 2)
{
sum += i;
}
Console.WriteLine(sum);
我会陆续更新C#学习笔记,从入门到进阶,感兴趣给以给点个赞支持一下! 点个关注,学习C#不迷路! 😀😀😀😀😀