顺序结构,分支结构
1. 数据类型的自动转换
参与运算(算术运算和赋值运算)的操作数和结果类型必须一致.当不一致时,满足下面条件时,系统自动完成类型转换(隐式转换)
1). 两种类型兼容
例如:int 和 double 兼容(都是数字类型),int可以转换成double,long,float,和decimal
2). 目标类型大于源类型(可以把小的转换成大的)
例如:double > int如果一个操作数为double型,则整个表达式可提升为double型,double转int显示转换,丢精度.int类型转化double类型可以,int类型变量*1.0
2. 强制转换
1)数值型之间的转换语法为:(带转换术语类型名)转换的值
比如:double不可以自动转换为int,这时候需要我们强制转换
double onsale=5.55;
int sale=(int)onsale;
2)数值型与非数值型之间的转换
比如:将string转换为int类型
string s;
int age=Convert.ToInt32(s);
这里需要注意的是:类型一旦确定就不会再改变,Convert只是改变那个变量值得类型。
这里的只是把s的值从string类型变为int类型,而s本身仍是string类型
3. 算术运算符+ - * / % ++ --
1) ++ 表示自加1,分为前加加和后加加(age++表示age=age+1)
相同点:不管是前加加还是后加加 ,结果就是变量的值都加了1;
不同点:如果是前加加,那么首先给这个变量的值自身加一,然后再参与运算。如果是后加加,那么首先拿原值参与运算,运算完成后,自身加一。
eg(后加): int age=18;
int sum=age++-18;
此时结果是:age=19,sum=8;
eg(前加).int age=18;
int sum=++ge-18;
此时结果是:age=19,sum=9;
2) - -表示自减1,分为前减和后减
相同点和不同点与++类似
3) 一元运算符:指的就是只需要一个操作数就能完成的运算 比如++,--
二元运算符:指的就是至少需要两个操作数才能完成的运算。
在一个表达式中,如果既有一元运算符,也有二元运算符,首先计算一元运算符,然后再算二元运算符。因为一元运算符的优先级要高于二元运算符。
4.复合赋值运算符
+=
int number=10;
number+=10; 即number=number+10;
-=
Number-=10; 即number=number-10;
此外,还有*= /= %=
5.关系运算符:是用来描述两个事物之间的关系,
> <</SPAN> >= <= == !=
由关系运算符连接的表达式叫做关系表达式,关系运算的结果是布尔类型的
布尔类型(bool):它的值只有两个true和false
Eg. int zsage=20,lsage=18;
bool isright=zsage
Console.WriteLine(isright);此时的结果为false,因为zsage>lsage
6.逻辑运算符有三种:
&&(逻辑与) ||(逻辑或) !(逻辑非)
逻辑运算符连接的表达式叫做逻辑表达式,要求逻辑表达式的的两边都要放关系表达式或者bool类型的值,也就是说表达式为true或者false的值
7.与或短路
1)与短路
Eg. int a=10,b=5;
bool result=++a>50&&++b>1
此时得到a=11,b=5 原因是:当++a>50为false的时候,++b便不再进行;只有当++a>50为 true的时候,才进行b++
2)或短路
Eg. int a=10,b=5;
bool result=++a>7||++b>1
此时得到a=11,b=5 原因是:当当++a>7为true的时候,++b便不再进行;只有当++a>7为false的时候,才进行b++
8.C#中异常捕获通过try{}catch{}来完成
语法为:
try
{
有可能出现的错误代码写在这里
}
catch
{
出错后的处理
}
说明:若是try中的代码没有出错,则程序正常进行try中的内容后,不会执行catch中的内容;若是try中的代码出错了,则立即执行catch中的内容,那么try出错之后的内容将不再进行。
Eg1.让用户输入一个数字,并显示出这个数字
Console.WriteLine("请输入一个数字");
try
{
int number = Convert.ToInt32(Console.ReadLine()); //将string类型转换成int类型
Console.WriteLine(number);
}
catch
{
Console.WriteLine("您输入有误");
}
当用户输入的是数字的时候,try中的内容不会出错,一直执行直到输出那个数字
当用户输入的不是数字的时候,try中内容:int number = Convert.ToInt32(Console.ReadLine()); 此时无法转换,则直接跳到catch中执行它里面的语句,此时就不再执行try之后的内容。
9.if结构的基本语法:
if(条件) //其中条件为关系表达式或逻辑表达式,它们产生的值为true或者false
{
语句块1;
}
else
{
语句块2;
}
执行过程:判断条件的结果;如果条件为true的时候,则执行语句块1;条件为false的时候,则跳过语句块1,执行else中的语句块2.
10.if else=if结构
只有当上一个条件不成立的时候,才会进行下一个if语句并进行if语句后面的条件判断,一旦有一个if后面的条件为true,则执行if所带的语句块,语句执行完成之后,程序跳出if else=if结构,若是所有if条件不成立,则如果最后有false则执行else所带的语句。
要注意的是: else永远是和离它最近的if配对
Eg. int age=15; char sex='f';
if (age < 10)
if (sex == 'f')
Console.WriteLine("小女人");
else
Console.WriteLine(age);
此时控制台上没有输出任何内容。因为age=15不满足 if (age < 10),所以不执行if (sex == 'f'),而这里边的else是和if (sex == 'f')配对的,所以这个程序不输入任何内容。
参与运算(算术运算和赋值运算)的操作数和结果类型必须一致.当不一致时,满足下面条件时,系统自动完成类型转换(隐式转换)
1). 两种类型兼容
例如:int 和 double 兼容(都是数字类型),int可以转换成double,long,float,和decimal
2). 目标类型大于源类型(可以把小的转换成大的)
例如:double > int如果一个操作数为double型,则整个表达式可提升为double型,double转int显示转换,丢精度.int类型转化double类型可以,int类型变量*1.0
2. 强制转换
1)数值型之间的转换语法为:(带转换术语类型名)转换的值
比如:double不可以自动转换为int,这时候需要我们强制转换
double onsale=5.55;
int sale=(int)onsale;
2)数值型与非数值型之间的转换
比如:将string转换为int类型
string s;
int age=Convert.ToInt32(s);
这里需要注意的是:类型一旦确定就不会再改变,Convert只是改变那个变量值得类型。
这里的只是把s的值从string类型变为int类型,而s本身仍是string类型
3. 算术运算符+ - * / % ++ --
1) ++ 表示自加1,分为前加加和后加加(age++表示age=age+1)
相同点:不管是前加加还是后加加 ,结果就是变量的值都加了1;
不同点:如果是前加加,那么首先给这个变量的值自身加一,然后再参与运算。如果是后加加,那么首先拿原值参与运算,运算完成后,自身加一。
eg(后加): int age=18;
int sum=age++-18;
此时结果是:age=19,sum=8;
eg(前加).int age=18;
int sum=++ge-18;
此时结果是:age=19,sum=9;
2) - -表示自减1,分为前减和后减
相同点和不同点与++类似
3) 一元运算符:指的就是只需要一个操作数就能完成的运算 比如++,--
二元运算符:指的就是至少需要两个操作数才能完成的运算。
在一个表达式中,如果既有一元运算符,也有二元运算符,首先计算一元运算符,然后再算二元运算符。因为一元运算符的优先级要高于二元运算符。
4.复合赋值运算符
+=
int number=10;
number+=10; 即number=number+10;
-=
Number-=10; 即number=number-10;
此外,还有*= /= %=
5.关系运算符:是用来描述两个事物之间的关系,
> <</SPAN> >= <= == !=
由关系运算符连接的表达式叫做关系表达式,关系运算的结果是布尔类型的
布尔类型(bool):它的值只有两个true和false
Eg. int zsage=20,lsage=18;
bool isright=zsage
Console.WriteLine(isright);此时的结果为false,因为zsage>lsage
6.逻辑运算符有三种:
&&(逻辑与) ||(逻辑或) !(逻辑非)
逻辑运算符连接的表达式叫做逻辑表达式,要求逻辑表达式的的两边都要放关系表达式或者bool类型的值,也就是说表达式为true或者false的值
7.与或短路
1)与短路
Eg. int a=10,b=5;
bool result=++a>50&&++b>1
此时得到a=11,b=5 原因是:当++a>50为false的时候,++b便不再进行;只有当++a>50为 true的时候,才进行b++
2)或短路
Eg. int a=10,b=5;
bool result=++a>7||++b>1
此时得到a=11,b=5 原因是:当当++a>7为true的时候,++b便不再进行;只有当++a>7为false的时候,才进行b++
8.C#中异常捕获通过try{}catch{}来完成
语法为:
try
{
有可能出现的错误代码写在这里
}
catch
{
出错后的处理
}
说明:若是try中的代码没有出错,则程序正常进行try中的内容后,不会执行catch中的内容;若是try中的代码出错了,则立即执行catch中的内容,那么try出错之后的内容将不再进行。
Eg1.让用户输入一个数字,并显示出这个数字
Console.WriteLine("请输入一个数字");
try
{
int number = Convert.ToInt32(Console.ReadLine()); //将string类型转换成int类型
Console.WriteLine(number);
}
catch
{
Console.WriteLine("您输入有误");
}
当用户输入的是数字的时候,try中的内容不会出错,一直执行直到输出那个数字
当用户输入的不是数字的时候,try中内容:int number = Convert.ToInt32(Console.ReadLine()); 此时无法转换,则直接跳到catch中执行它里面的语句,此时就不再执行try之后的内容。
9.if结构的基本语法:
if(条件) //其中条件为关系表达式或逻辑表达式,它们产生的值为true或者false
{
语句块1;
}
else
{
语句块2;
}
执行过程:判断条件的结果;如果条件为true的时候,则执行语句块1;条件为false的时候,则跳过语句块1,执行else中的语句块2.
10.if else=if结构
只有当上一个条件不成立的时候,才会进行下一个if语句并进行if语句后面的条件判断,一旦有一个if后面的条件为true,则执行if所带的语句块,语句执行完成之后,程序跳出if else=if结构,若是所有if条件不成立,则如果最后有false则执行else所带的语句。
要注意的是: else永远是和离它最近的if配对
Eg. int age=15; char sex='f';
if (age < 10)
if (sex == 'f')
Console.WriteLine("小女人");
else
Console.WriteLine(age);
此时控制台上没有输出任何内容。因为age=15不满足 if (age < 10),所以不执行if (sex == 'f'),而这里边的else是和if (sex == 'f')配对的,所以这个程序不输入任何内容。
if 结构
if (条件)
{
//成立,执行语句块
}
If-else
if (条件)
{
语句块1;
}
else
{
语句块2;
}
{
语句块1;
}
else
{
语句块2;
}
If else if
if (条件1)
{
语句块1;
}
else if(条件2)
{
语句块2;
}
{
语句块1;
}
else if(条件2)
{
语句块2;
}
else if(条件3)
{
语句块3;
}
{
语句块3;
}
....
//3-12
Console.WriteLine("请输入你的用户名");
string username=Console.ReadLine();
Console.WriteLine("请输入你的密码");
string password=Console.ReadLine();
if(username=="admin"&&password=="888888")
{
Console.WriteLine("登陆成功");
}
else if(username!="admin")
{
Console.WriteLine("用户名不存在");
}
else if(password!="888888"))
{
Console.WriteLine("您的密码不正确");
}
switch case分支结构
二、使用标志(***注意理解,并能灵活运用***)
//3-14<h2><span style="font-family:Arial">switch</span></h2>练习
Console.WriteLine("请输入对李四的年终工作评定");
string input=Console.ReadLine();
decimal salary=5000;
bool flag=false;
switch(input)
{
case "A":
salary+=500;
break;
case "B":
salary+=200;
break;
case "C":
break;
case "D":
salary-=200;
break;
case "E":
salary-=500;
break;
default:
Console.WriteLine("您的输入不正确");
flag=ture;
break;
}
if(flag==false)
{
Console.WriteLine("李四的来年年工资为"+salary);
}
四、default 的位置
If else if 和switch的比较
相同点:都可以实现多分支结构
不同点:
Switch:一般 只能用于等值比较,根据计算switch后面的表达式的值与case后面的每一个进行匹配
If—else if:可以处理范围,必须在else if 可以进行相当以及不等的比较,但必须在else if 后面加以条件
while 循环
一、循环结构介绍
//4-2
Console.WriteLine("请输入班级人数");
int people=Convert.ToInt32(Console.ReadLine());
int i=0,score=0;
while(i<people)
{
Console.WriteLine("请输入学员成绩");
int score=Convert.ToInt32(Console.ReadLine());
score+=score;
i++;
}
Console.WriteLine("本班的平均成绩为{0},平均成绩为{1}",score/people,score);
do-while 循环
//do-while-2
do
{
Console.WriteLine("请输入你的用户名");
string username=Console.ReadLine();
Console.WriteLine("请输入你的密码");
string password=Console.ReadLine();
}
while(username!="admin"||password!="888888");
//do-whlie-3
do
{
Console.WriteLine("请输入学生姓名(输入q结束)");
string input=Console.ReadLine();
}
while(input=="q");
//do-while-4
do
{
Console.WriteLine("请输入一个数字");
string input=Console.ReadLine();
int number=Convert.ToInt32(input);
Console.WriteLine("您刚刚输入的数字是"+number*2);
}
while(input=="q");
//do-while-5
int max=0;
do
{
Console.WriteLine("请输入一个数字");
string input=Console.ReadLine();
int number=Convert.ToInt32(input);
if(number>max)
{
max=number;
}
}
while(input=="end");
Console.WriteLine("您刚刚输入的数字是"+max);
三、区别对比
for循环
四、与while对比学习
//for-2
int all=0;
for(i=0;i<100;i++)
{
if(i%2=0)
{
all=all+i+1;
}
i++
}
Console.WriteLine("1到100的奇数和是"+all+1);
//for-5
for(i=0;i<=9;i++)
{
for(j=0;j<=i;j++)
{
Console.Write("{0}*{1}={2:00}",i,j,i*j);
}
Console.WriteLine();
}
五、举例
2.1-999之间的水仙花数
//for-3
int a=i%10;
int b=i/10%10;
int c=i/100;
for(i=100;i<1000;i++)
{
if(i==a*a*a+b*b*b+c*c*c)
{
Console.WriteLine("100到999的水仙花数是"+i);
}
}
3. For嵌套
//for-6
for(i=6;i>=0;i--)
{
for(j=0;j<=6;j++)
{
Console.WriteLine("{0}+{1}={2:00}",j,i,i+j);
}
}
4. 省略调试
语法错误
逻辑错误
一、设置断点
黄色语句块:将执行,还未执行
逐语句f11 逐过程f10
右键---快速监视
二、单步运行
三、观察变量
7练习题
Console.WriteLine("请输入你的名字");
string name=Console.ReadLine();
Console.WriteLine("请输入你的语文成绩");
int chinese=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你的数学成绩");
int math=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你的英语成绩");
int english=Convert.ToInt32(Console.ReadLine());
int score=chinese+math+english;
Console.WriteLine(" {0}你的总分为{1},平均分为{2}。",name,score,score/3);
//2-8
Console.WriteLine(请输入你要查询的天数);
int days=Convert.ToInt32(Console.ReadLine());
int week=days/7;
int day=days%7;
Console.WriteLine("46天有{0}周,余{1}天",week,day);
//2-9
Console.WriteLine(请输入你要查询的秒数);
int time=Convert.ToInt32(Console.ReadLine());
int day=time/(60*60*24);
int hour=time%(60*60*24)/(60*60);
int min=time%(60*60)/60;
int sec=time%60;
Console.WriteLine("{0}秒有{1}天{2}小时{3}分{4}秒",time,day,hour,min,sec);
Console.ReadKey();
//3-2
Console.WriteLine("请输入你要查询的年份数");
int year=Convert.ToInt32(Console.ReadLine());
bool dec=(year%400==0||year%4==0&&year%100!=0)
Console.WriteLine("您所查询的年份{0}闰年",dec)
//3-17
Console.WriteLine("请输入你要查询的年份");
int year=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你要查询的月份");
int month=Convert.ToInt32(Console.ReadLine());
bool sec=(year%400==0||year%4==0&&year%100!=0)
switch(month)
{
case 2:
if(sec)
{
Console.WriteLine("您所查询的{0}年{1}月有28天",year,month);
}
else
{
Console.WriteLine("您所查询的{0}年{1}月有29天",year,month);
}
break;
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("您所查询的{0}年{1}月有31天",year,month);
break;
case 4:
case 6:
case 9:
case 11:
Console.WriteLine("您所查询的{0}年{1}月有30天",year,month);
break;
default:
Console.WriteLine("您所查询的{0}年{1}月不存在!",year,month);
break;
Console.ReadKey();
//4-3
int i=0;
while(answer=="n"&&i<10)
{
Console.WriteLine("老师讲一遍题,问这道题你会做了么(y/n)");
string answer=Console.ReadLine();
i++;
}
Console.WriteLine("好了,下课休息");
//4-4
int stu=80000,year=2006;
while(stu>=200000)
{
stu=stu*1.25
year++
}
Console.WriteLine("到{0}年学员人数超过20万人",year);
//预习7-1
bool flag = true;
int eve = 0;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("请输入你要计算的第{0}个人的年龄", i + 1);
int old = Convert.ToInt32(Console.ReadLine());
eve = eve + old;
if (old > 100 || old < 0)
{
Console.WriteLine("你的输入有误");
flag = false;
break;
}
}
if (flag == true)
{
Console.WriteLine("这五个人的平均年龄是{0}", eve / 5);
}
//预习7-2
string username = "", password = "";
while(true)
{
Console.WriteLine("请输入你的用户名");
username = Console.ReadLine();
Console.WriteLine("请输入你的密码");
password = Console.ReadLine();
if (username == "admin" && password == "888888")
{
Console.WriteLine("chenggong");
break;
}
}
for(i=2;i<=99;i++)
{
i%x!=0
}
Console.WriteLine("");
Console.ReadKey();