2.把数变化到某个范围之内。——彩票生成。
3.判断能否整除。——闰年、平年。
inta=10,b=3;Console.WriteLine("10/3="+(a/b));Console.WriteLine("10%3="+(a%b));++(自增运算)--(自减运算)——它只能对变量进行运算。
inta=5;a++;Console.WriteLine(a);//a=6;1.前自增/前自减先进行自增/自减运算,然后再进行其它运算。可以简单认为前自增/前自减的优先级是最高。
inta=5,b;b=++a;Console.WriteLine("a="+a+";b="+b);//结果:a=6,b=62.后自增/后自减先进行其它运算,当其它运算都完成后,再进行自增/自减运算。可以简单认为是后自增/后自减优先级是最低的。
inta=5,b;b=a++;Console.WriteLine("a="+a+";b="+b);//结果:a=6,b=5
(二)、关系运算符:——用来判断式子成立与否
注意:双等号不要写成单等号
inta=5,b=6;Console.WriteLine(a>b&&a>0);//false;Console.WriteLine(a0);//true
inta=5,b=6;Console.WriteLine((a>b)||(a>0));//trueConsole.WriteLine((a>b)||(a<0));//false
!非——取反
优先级:一般来说:1.算术运算术的优先级要高关系运算符;关系运算符的优先级要高于逻辑运算符2.逻辑非优先级最高。逻辑与要高于逻辑或。3.如果不确定,就加小括号。
1.赋值运算符:=。把右边的结果送到左边去。左边只能是变量。
2.复合运算符:+=-=*=/=%=知道就行。a+=5;<==>a=a+5
3.条件运算符:三目运算符:。
inta=5,b=6,c;
c=a>ba:b;
Console.WriteLine(c)//6
作业:
1.游泳池一游泳场地,呈圆形,泳池半径a,广场半径b(a
①求泳池护栏长度②求广场砖面积③护栏造价25元/米,广场砖85元/m^2,求护栏、广场砖共花费多少?
Console.Write("请输入泳池半径a:");stringa=Console.ReadLine();Console.Write("请输入广场半径b:");stringb=Console.ReadLine();constdoublePI=3.1415926;doubleL=2*PI*Convert.ToDouble(a);doubleS1=PI*Convert.ToDouble(b)*Convert.ToDouble(b)-PI*Convert.ToDouble(a)*Convert.ToDouble(a);doubleRMB=25*L+85*S1;Console.WriteLine("泳池护栏长度L:"+L+"m");Console.WriteLine("广场砖面积S1:"+S1+"m^2");Console.WriteLine("护栏、广场砖共花费YMB:"+RMB+"元");
2.老狼老狼几点了
Console.Write("老狼老狼几点了?");stringhour=Console.ReadLine();inth,h1;stringap;h=Convert.ToInt32(hour);h1=h>12h-12:h;ap=h>12"下午":"上午";Console.WriteLine("现在是"+ap+h1+"点!");
3.输入三个数a,b,c。输出最大的。
inta,b,c,d,max;Console.WriteLine("请输入三个数");stringa1=Console.ReadLine();stringb1=Console.ReadLine();stringc1=Console.ReadLine();a=Convert.ToInt32(a1);b=Convert.ToInt32(b1);c=Convert.ToInt32(c1);d=a>ba:b;max=d>cd:c;Console.WriteLine("最大值是"+max);
判断--表达式。if(){}
四大类:1.ifif(age>18){Console.WriteLine("可以去当兵!");}
注意:if表达式后面只管一句话,可以省略掉{};如果if表达式后面需要管多句话,则必须加{}
2.if...else...
if(age>18){Console.WriteLine("成年了!");Console.WriteLine("可以去当兵!");}else{Console.WriteLine("还没长大!");Console.WriteLine("回家上学去!");}
注意:1.else后面不要加分号。2.else后面不要加小括号。
3.if...elseif...elseif...else多分支。
4.if嵌套。
if(...){if(...){}else{}}else{if(...){}else{}}
分层、分类来解决问题的思路。
作业:1.老狼几点了。凌晨,上午,下午,晚上。
2.判断一元二次方向根的情况。
Console.WriteLine("请输入方程系数a、b、c");strings=Console.ReadLine();strings1=Console.ReadLine();strings2=Console.ReadLine();inta,b,c;doubled;a=Convert.ToInt32(s);b=Convert.ToInt32(s1);c=Convert.ToInt32(s2);d=b*b-4*a*c;if(a==0){Console.WriteLine("不是一元二次方程");}elseif(d>0){Console.WriteLine("有两个实根");}elseif(d<0){Console.WriteLine("没有实根");}else{Console.WriteLine("有一个实根");}
3.输入一个年份,判断是闰年还是平年。
Console.Write("请输入年份:");strings=Console.ReadLine();intyear=Convert.ToInt32(s);if(year>0&&year<9999){if(year%400==0){Console.WriteLine(year+"年是闰年");}elseif(year%4==00&&year%100!=0){Console.WriteLine(year+"年是闰年");}else{Console.WriteLine(year+"年是平年");}}else{Console.WriteLine("输入年份有问题,请核对");}
4.称体重。男人的标准体重是:体重(kg)=身高(cm)-100。女人的标准体重是:体重(kg)=身高(cm)-110。上下浮动3公斤属正常要求输入性别、身高和体重,输出正常,偏胖,偏瘦
Console.Write("请输入性别:");stringsex=Console.ReadLine();Console.Write("请输入身高:");stringh=Console.ReadLine();Console.Write("请输入体重");stringw=Console.ReadLine();inth1,w1,b,n;h1=Convert.ToInt32(h);w1=Convert.ToInt32(w);if(sex=="男"){n=h1-100;b=w1-n;if(b>=-3&&b<=3){Console.WriteLine("体重正常");}elseif(b<-3){Console.WriteLine("略瘦");}else{Console.WriteLine("略胖");}}elseif(sex=="女"){n=h1-110;b=w1-n;if(b>=-3&&b<=3){Console.WriteLine("体重正常");}elseif(b<-3){Console.WriteLine("体重偏瘦");}else{Console.WriteLine("偏胖");}}else{Console.WriteLine("性别输入错误");}
//称体重(方法2)
Console.Write("请输入性别:");stringsex=Console.ReadLine();Console.Write("请输入身高:");stringh=Console.ReadLine();Console.Write("请输入体重");stringw=Console.ReadLine();inth1,w1,b,n=0;h1=Convert.ToInt32(h);w1=Convert.ToInt32(w);if(sex=="男"||sex=="女"){if(sex=="男"){n=h1-100;}elseif(sex=="女"){n=h1-110;}b=w1-n;if(b>=-3&&b<=3){Console.WriteLine("体重正常");}elseif(b<-3){Console.WriteLine("略瘦");}else{Console.WriteLine("略胖");}}else{Console.WriteLine("性别输入错误");}
5.输入年、月、日,判断是否是个正确的日期。
Console.Write("请分别输入年月日");stringyear1=Console.ReadLine();stringmonth1=Console.ReadLine();stringday1=Console.ReadLine();intyear=Convert.ToInt32(year1);intmonth=Convert.ToInt32(month1);intday=Convert.ToInt32(day1);if(year>0&&year<9999){if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){if(day>0&&day<=31){Console.WriteLine("日期正确");}else{Console.WriteLine("日期错误");}}elseif(month==4||month==6||month==11||month==9){if(day>0&&day<=30){Console.WriteLine("日期正确");}else{Console.WriteLine("日期错误");}}elseif(month==2){if(year%400==0||year%4==00&&year%100!=0){if(day>0&&day<=29){Console.WriteLine("日期正确");}else{Console.WriteLine("日期错误");}}else{if(day>0&&day<=28){Console.WriteLine("日期正确");}else{Console.WriteLine("日期错误");}}}else{Console.WriteLine("日期错误");}}else{Console.WriteLine("日期错误");}