运算符:
一、算术运算符:
+ - * / % ——取余运算
取余运算的应用场景:
1.奇偶数的区分。
2.把数变化到某个范围之内。——彩票生成。
3.判断能否整除。——闰年、平年。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
//输入一个年份判断是闰年,还是平年。
static void ccc(string[] args)
{
int year;
//输入
Console.Write("请输入一个年份:");
year = Convert.ToInt32(Console.ReadLine()); //运算
//能被400整除;或能被4整除,但不能被100整除。
if ((year % == ) || (year % == && year % != ))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("是平年");
}
}
}
}
++(自增运算) --(自减运算)——它只能对变量进行运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = ;
a++;
//7++; //错误。
Console.WriteLine(a);//a = 6;
}
}
}
1.前自增/前自减
先进行自增/自减运算,然后再进行其它运算。可以简单认为前自增/前自减的优先级是最高。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b;
b = ++a;
Console.WriteLine("a=" + a + ";b=" + b); //结果应当a=6,b=6
}
}
}
2.后自增/后自减
先进行其它运算,当其它运算都完成后,再进行自增/自减运算。可以简单认为是后自增/后自减优先级是最低的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b;
b = a++;
Console.WriteLine("a=" + a + ";b=" + b);//结果应当是a=6,b=5
}
}
}
二、关系运算符:——用来判断式子成立与否
== != > >= < <=
注意:
双等号不要写成单等号
三、逻辑运算符:&&,||都双操作数,!单操作数
&& 与(并且)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b = ;
Console.WriteLine(a > b && a > ); //false;
//true???
}
}
}
|| 或(或者)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = , b = ;
Console.WriteLine((a > b) || (a > )); //true
//false??
}
}
}
! 非 ——取反
优先级:
一般来说:
1.算术运算术的优先级要高关系运算符;关系运算符的优先级要高于逻辑运算符
2.逻辑非优先级最高。逻辑与要高于逻辑或。
3.如果在不确定,就加小括号。
四、其它运算符:
1.赋值运算符:=。把右边的结果送到左边去。左边只能是变量。
2.复合运算符:+= -= *= /= %= 知道就行。
a+=5; <==> a = a + 5
3.条件运算符:三目运算符 ?: 。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a=,b=,c;
c = a > b ? a : b;
Console.WriteLine( c ) }
}
}
作业:
1.游泳池
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
const int BAR_UNIT_PRICE = ;
const int BRICK_UNIT_PRICE = ;
//输入
int a, b;
Console.Write("请输入泳池半径:");
string s1 = Console.ReadLine();
a = Convert.ToInt32(s1);
Console.Write("请输入广场半径:");
string s2 = Console.ReadLine();
b = Convert.ToInt32(s2); //运算
double l = * PI * a; //求泳池的周长
double area1 = PI * a * a;//泳池的面积。
double area2 = PI * b * b;//总面积。
double area = area2 - area1;//广场面积
double barPrice = l * BAR_UNIT_PRICE; //护栏的总造价
double brickPrice = area * BRICK_UNIT_PRICE;//广场的造价。 //输出
Console.WriteLine("护栏的长度是" + l + "米,广场砖的总面积是" + area + "平米,总造价为" + (barPrice + brickPrice) + "元"); }
}
}
2.老狼老狼几点了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("老狼老狼几点了?");
string hour = Console.ReadLine();
int a = Convert.ToInt32(hour ),b;
string c; b = a > ? a - : a;
c = a > ? "下午" : "上午";
Console.WriteLine("现在是"+c+b+"点了"); }
}
}
3.输入三个数a,b,c。输出最大的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
int a, b, c,d,max;
Console.Write("请输入三个数:");
string a1 = Console.ReadLine();
string b1 = Console.ReadLine();
string c1 = Console.ReadLine(); a = Convert.ToInt32(a1);
b = Convert.ToInt32(b1);
c = Convert.ToInt32(c1);
d = a > b ? a : b;
max = d > c ? d : c;
Console.WriteLine("三个数中最大的数是"+max); }
}
}
二、语句:
顺序,分支,循环。
(一)顺序:略
分支:判断--表达式。if(){}
四大类:
.if
if (age > 18)
{
Console.WriteLine("可以去当兵!");
}
注意:if表达式后面只管一句话,可以省略掉{};如果if表达式后面需要管多句话,则必须加{}
.if...else...
if (age > 18)
{
Console.WriteLine("成年了!");
Console.WriteLine("可以去当兵!");
}
else
{
Console.WriteLine("还没长大!");
Console.WriteLine("回家上学去!");
}
注意:
1.else后面不要加分号。
2.else后面不要加小括号。
.if...else if...else if...else 多分支。
.if嵌套。
if(...)
{
if(...)
{
}
else
{
}
}
else
{
if(...)
{
}
else
{
}
}
分层、分类来解决问题的思路。
练习:
1.老狼几点了。凌晨,上午,下午,晚上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
//输入
Console.Write("老狼老狼几点了?");
string s = Console.ReadLine();
int hour = Convert.ToInt32(s); if (hour >= && hour < ) // 0<hour<6:错误
{
Console.WriteLine("凌晨" + hour + "点了");
}
else if (hour >= && hour <= )
{
Console.WriteLine("上午" + hour + "点了");
}
else if (hour > && hour < )
{
hour -= ;
Console.WriteLine("下午" + hour + "点了");
}
else if (hour >= && hour < )
{
hour -= ;
Console.WriteLine("晚上" + hour + "点了");
}
else
{
Console.WriteLine("不可识别的时间!");
} }
}
}
2.判断一元二次方向根的情况。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
//判断一元二次方程根的情况。
static void Main(string[] args)
{
int a, b, c;
//输入;
Console.Write("请输入系数a:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入系数b:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入系数c:");
c = Convert.ToInt32(Console.ReadLine()); //运算输出
if (a == )
{
Console.WriteLine("不是一元二次方程");
}
else
{
int d = b * b - * a * c;
if (d > )
{
Console.WriteLine("两个不等实根");
}
else if (d == )
{
Console.WriteLine("两个相等实根");
}
else
{
Console.WriteLine("无实根");
}
}
}
}
}
3.输入一个年份,判断是闰年还是平年。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
//输入一个年份判断是闰年,还是平年。
static void Main(string[] args)
{
int year;
//输入
Console.Write("请输入一个年份:");
year = Convert.ToInt32(Console.ReadLine()); //运算
//能被400整除;或能被4整除,但不能被100整除。
if ((year % == ) || (year % == && year % != ))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("是平年");
}
}
}
}
4.称体重。
男人的标准体重是:体重(kg)=身高(cm)-100。
女人的标准体重是:体重(kg)=身高(cm)-110。
上下浮动3公斤属正常
要求输入性别、身高和体重,输出正常,偏胖,偏瘦
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class3
{
static void Main(string[] args)
{
string sex;
int weight, height;
//输入
Console.Write("性别(男,女):");
sex = Console.ReadLine();
Console.Write("身高(CM):");
height = Convert.ToInt32(Console.ReadLine());
Console.Write("体重(KG):");
weight = Convert.ToInt32(Console.ReadLine()); int biaoZhunTiZhong=;
//运算输出
if(sex == "男")
{
//求标准体重
biaoZhunTiZhong = height - ;
}
else if(sex == "女")
{
//求标准体重
biaoZhunTiZhong = height - ;
}
else
{
Console.WriteLine("性别不正确");
} //求体重差
int tiZhongCha = weight - biaoZhunTiZhong;
if (tiZhongCha >= - && tiZhongCha <= )
{
Console.WriteLine("体重正常,继续保持!");
}
else if (tiZhongCha < -)
{
Console.WriteLine("偏瘦,注意增加营养");
}
else
{
Console.WriteLine("偏胖,注意运动减肥");
}
}
}
}
5.输入年、月、日,判断是否是个正确的日期。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class4
{
static void Main(string[] args)
{
int year = , month = , day = ;
//输入
Console.Write("请输入年:");
year = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入月:");
month = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入日:");
day = Convert.ToInt32(Console.ReadLine()); //判断运算输出
//判断年份是否正确
if(year < || year>)
{
Console.WriteLine("年输入不正确");
}
else
{
Console.WriteLine("年正确");
} //判断月份是否正确
if (month < || month > )
{
Console.WriteLine("月输入不正确");
}
else
{
Console.WriteLine("月正确");
} //判断天是否正确
if(month==||month==||month==||month==||month==||month==||month==)
{
if(day < || day>)
{
Console.WriteLine("天输入错误,大月份最多是31天");
}
else
{
Console.WriteLine("天正确");
}
}
else if (month == || month == || month == || month == )
{
if (day < || day > )
{
Console.WriteLine("天输入错误,小月份最多是30天");
}
else
{
Console.WriteLine("天正确");
}
}
else if(month == )
{
//闰年与平年的判断
if(year%== || year%==&&year%!=)
{
//闰年
if (day < || day > )
{
Console.WriteLine("天输入错误,闰年2月份最多是29天");
}
else
{
Console.WriteLine("天正确");
}
}
else
{
//平年
if (day < || day > )
{
Console.WriteLine("天输入错误,平年2月份最多是28天");
}
else
{
Console.WriteLine("天正确");
}
}
}
}
}
}