【Language】C#之旅_②_表达式基本应用
来源:互联网 发布:mysql数据库连不上 编辑:程序博客网 时间:2024/06/05 22:41
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/*表达式应用*/
//运算符优先级:
//元运算符:(x)、x.y、f(x)、a[x]、x++、x--、new、typeof、sizeof、checked、uncheck
//一元运算符:+、-、~!、++x、--x、(T)x
//算术运算符:+、-、*、/、%
//位运算符:<<、>>、&、|、^、~
//关系运算符:<、>、<=、>=、is、as
//逻辑运算符:&、|、^
//条件运算符:&&、||、?
//赋值运算符:=、+=、-=、*=、/=、<<=、>>=、&=、^=、|=
//除了赋值运算符之外其他运算符都是左结合
//算术运算符
int a = 10;
int b = 20;
int c = 3;
int x = a + b;
int y = 1 + 4;
int z = 1 + a;
int p = b - a;
int q = b / y;
int t = a * c;
int u = a % c;
int v = a++;
int w = ++b;
Console.WriteLine("a+b="+x);
Console.WriteLine("1+4="+y);
Console.WriteLine("1+a="+z);
Console.WriteLine("b-a="+p);
Console.WriteLine("b/5="+q);
Console.WriteLine("a*c="+t);
Console.WriteLine("a%c="+u);
Console.WriteLine("a++="+v+"此时a="+a);
Console.WriteLine("++b="+w);
//关系运算符
string a1 = "hello";
string a2 = "hello";
string a3 = "hey";
if (a1 == a2)
{
Console.WriteLine("相等");
Console.WriteLine((a1 == a2).ToString());
}
else
{
Console.WriteLine("不相等");
Console.WriteLine((a1 == a2).ToString());
}
if (a1 == a3)
{
Console.WriteLine("相等");
Console.WriteLine((a1 == a3).ToString());
}
else
{
Console.WriteLine("不相等");
Console.WriteLine((a1 == a3).ToString());
}
//逻辑运算符
bool myBool = true;
bool notTrue = !myBool;
bool yu = myBool && notTrue;
bool oth = true;
if (yu && oth)
{
Console.WriteLine("真");
}
else
{
Console.WriteLine("假");
}
int res = 1;
if (Convert.ToBoolean(res))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
//位运算符
int resu = 4;
Console.WriteLine((resu << 1).ToString());
Console.WriteLine((resu & res).ToString());
Console.WriteLine((~resu).ToString());
//赋值运算符
int b1, b2, b3,b4;
b1 = b2 = b3 = b4=1;
b1 += 1;
b2 = b2 + 1;
b3 <<= 1;
b4 &= 1;
Console.WriteLine(b1+" "+b2+" "+b3+" "+b4);
//条件运算符
bool c1 = true;
string c2 = c1 ? "true" : "false";
Console.WriteLine(c2.ToString ());
Console.ReadKey();//等待user输入
}
}
}
- 【Language】C#之旅_②_表达式基本应用
- 【Language】C#之旅_①_简要变量规则
- 【Language】C#之旅_③_条件语句
- 【Language】C#之旅_④_循环语句
- 【Language】C#之旅_①_简要变量规则
- java学习之旅60--数组_多维数组_基本语法_内存分析
- 简单正则表达式的应用_只包含_
- .net学习之C#基础提高 14.逻辑表达式关系表达式习题_逻辑短路
- 栈的应用_简单表达式求值
- C#网络编程001_基本类
- java学习之旅10--基本数据类型自动转换_强制转型_类型提升问题
- 常见对象_正则表达式的概述和基本使用
- C#之入门总结_继承_06
- C#之入门总结_多态_07
- C#之入门总结_字符串_09
- C#之入门总结_集合_10
- C#之入门总结_ 异常_11
- C#之入门总结_文件_1_13
- 一个29岁的总裁对大学生的忠告
- Refactor
- reactos操作系统实现(186)
- Flex 字体使用
- 隐藏域方式的Ajax实现
- 【Language】C#之旅_②_表达式基本应用
- 重温基础-提高软件质量
- C语言
- 新手来报道~
- NFS 网络文件系统/ARM-LINUX NFS服务启动设置
- Eclipse远程调试Tomcat
- 集群技术
- HTTP响应码与HTTP头标
- 读可扩展web架构ppt的笔记