C#入门 11~13

本文介绍了C#中两种字符串拼接方法,包括使用`+`运算符和`string.Format`函数,并通过示例展示了它们的区别和用法。接着,讲解了条件运算符,如大于、小于、等于等,并演示了不同类型之间的比较。最后,讨论了逻辑运算符`&&`、`||`和`!`的用法,以及短路规则。

lesson 11 字符串拼接

知识点一 :字符串拼接方法1

            //之前的算术运算符 只是用来数值类型变量进行数学运算的
            //而 string 不存在算术运算符 不能计算 但是可以通过+号来进行字符串拼接 

string str="123";

str=str+"456";

            //string 也能用上 复合运算符 +=

str="123";

str+="1"+4+"true";

            //区别
            str = "123";
            str += 1 + 2 + 3 + 4;
            Console.WriteLine(str); //输出 12310
            str = "123";
            str += ""+1 + 2 + 3 + 4;
            Console.WriteLine(str); // 1231234
            str = "";
            str += 1 + 2 + "" + 3 + 4;
            Console.WriteLine(str); // 334
            str = "";
            str += 1 + 2 + "" + (3 + 4);
            Console.WriteLine(str); //37
            str = str + 1 + 2 + 3;
            Console.WriteLine(str); //37123
            str = "123";
            str = str + 1 + 2 + 3;
            Console.WriteLine(str); //123123
            str = "123";
            str += 1 + 2 + 3;
            Console.WriteLine(str);//1236

//注意 : 用+号拼接 是用符号唯一方法 不能用-*/%等等

知识点二 字符串拼接方法2

            //固定语法
            //string.Format("待拼接的内容",内容1, 内容2,......);
            //拼接内容中的固定规则
            //想要被拼接的内容用占位符替代 {数字} 数字:0~n 依次往后

            string str2;
            str2=string.Format("我是{0},我今年{1}岁,我要{2}", "盛夏", 18, "好好学习");
            Console.WriteLine(str2);

            str2 = string.Format("{0}{1}{2}{3}", 1, true, false, 1.1);
            Console.WriteLine(str2);

控制台打印拼接

            //后面的 内容 比占位符多 不会报错
            //后面的 内容 比占位符少 会报错
            Console.WriteLine("A{0}B{1}C{2}D{3}", 1, true, false, 1.1);
            Console.Write("A{0}B{1}C{2}D{3}", 1, true, false, 1.1);

lesson 12 条件运算符

知识点一  条件运算符

 //用于比较两个变量或常量
 //是否大于 >
 //是否小于 <
 //是否等于 ==
 //是否不等于 !=
 //是否大于等于 >=
 //是否小于等于 <=

 //条件运算符 一定存在左右两边的内容
 //左边内容 条件运算符 右边内容

int a = 1;
int b = 2;
//条件运算符不能直接这样使用
//纯比较不用结果 那么对于我们来说 没有任何意义
// 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);

知识点二  各种应用写法

//变量和变量比较
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
//计算结果比较
//条件运算符的 优先级 低于算术运算符
//先计算 再比较
result = a + 3 > a - 2 + 3;//true
result = 3 + 3 < 5 - 1;//false
 

知识点三  不能进行范围比较

//判断是否在某两个值之间
//1<a<6
//在C#都不能这样写
//result = 1 < a < 6;
//要判断 一个变量是否在两个数之间 要结合 逻辑运算符的知识点
 

知识点四  不同类型之间的比较

//不同数值类型之间 可以随意进行条件运算符比较
int i = 1;
float f = 1.1f;
double d = 1.2;
short s = 2;
byte by = 3;
uint ui = 5;

//只要是数值 就能够进行条件运算符比较 比较大于小于等于等待
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
//char 不仅可以和自己类型进行 == != 还可以和数值类型进行比较
//还可以和 字符类型进行大小比较
result = c > 123;
result = c > 'B';

result = bo == true;//true
 

练习

bool gameOver, startGame;
int a = 10, b = 15;
gameOver = a > (b - 5);
startGame = gameOver == (b > (a + 5));//true
Console.WriteLine("startGame=" + startGame);
 

lesson 13 逻辑运算符

//对bool 类型  进行逻辑运算

知识点一  逻辑与

//符号 &&
//规则 对两个bool值进行逻辑运算 :有假则假 同真为真
bool result = true && false;
Console.WriteLine(result);
result = true && true;
Console.WriteLine(result);

//bool相关的类型 bool变量 条件运算符
//逻辑运算符优先级 低于 条件运算符 算术运算符
result = 3 > 1 && 1 < 2;//true && true
Console.WriteLine(result);

int i = 3;
//1<i<5;
result = i > 1 && i < 5;//true && true
Console.WriteLine(result);

//多个逻辑与 组合运用
int i2 = 5;
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 | false;
Console.WriteLine(result);

result = 3 > 10 || 3 < 5;
Console.WriteLine(result);

int a = 5;
int b = 11;
result = a > 1 || b < 20 || a > 5;//true || true || false
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);
 

知识点四  混合使用优先级问题

//规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于 ||(逻辑或)
//逻辑运算符优先级 低于 算术运算符 条件运算符(逻辑非除外)
bool gameOver = false;
int hp = 100;
bool isDead = false;
bool isMustOver = true;
result = gameOver || hp + 100 < 0 && !isDead || isMustOver;
//false || false && true || true
//false ||false || true
Console.WriteLine(result);
 

知识点五  逻辑运算符短路规则

int i3 = 1;
// || 有真则真
//只要 逻辑与 或者 逻辑或 左边满足了条件
//i3 > 0  为true
//只要 满足条件 右边的内容 对于我们来说 已经不重要了 所以不会进行计算
result = i3 > 0 || ++i3 >= 1;
Console.WriteLine(result);
Console.WriteLine(i3);

//false && i3++ > 1 
//逻辑与 有假则假 左边只要为假 右边则不再计算
result = i3 < 0 && i3++ > 1;
Console.WriteLine(result);
Console.WriteLine(i3);

//当左边不能确定时 右边才会进行计算
 

练习

//求打印结果
bool gameOver;
bool isWin;
int health = 100;
gameOver = true;
isWin = false;
Console.Write(gameOver || isWin && health > 0);//true
 


 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值