课堂回顾
常用常见的变量类型
整型 浮点型 数组 布尔型 字符串型
变量的命名规则
不能以数字开头,由大小写字母,数字,下划线组成
Read与ReadLine
Console.Read();//返回的是字符
Console.ReadLine();//返回的是字符串
Console.WriteLine();//有默认的换行符
Console.Write("fdf");//没有自带的换行符
运算符
运算符 + - * / %
赋值运算符== = /= += -= %= a=b=>a=a*b
关系运算符 > >= <= < != ==
逻辑运算符 &&与 ||或 !=非取反 结果就是一个布尔值
Console.WriteLine(true && false);
三目运算符 式子? 值1:值2 当式子的结果是真那么等于值1,否则就是值2
类型转换 强转 Convert
string str = "2";
int num = 1;
num = Convert.ToInt32(str);
条件分支
双分支 两种情况
if (判断条件)
{
执行语句1;//当条件为真则执行此语句
}
else
{
执行语句2;
}
//多分支 很多种情况
if (num > 2)
{
Console.WriteLine(1);
}
else if (num > 3)
{
Console.WriteLine(2);
}
else if (num > 4)
{
Console.WriteLine(3);
}
else
{
Console.WriteLine(4);
}
if (num == 2) Console.Write(2);//如果只有一条执行语句的时候括号可以省略
Console.WriteLine(num);
练习 考试成绩评判等级
Console.WriteLine("请输入考试成绩");
int score = Convert.ToInt32(Console.ReadLine());
if (score >= 90 && score <= 100)
{
Console.WriteLine("A");
}
else if (score >= 70 && score <= 89)
{
Console.WriteLine("B");
}
else if (score >= 60 && score <= 69)
{
Console.WriteLine("C");
}
else if (score < 60)
{
Console.WriteLine("D");
}
}
}
判断x,y处于第几象限
Console.WriteLine("请输入x")
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入y");
int y = Convert.ToInt32(Console.ReadLine());
if (x > 0 && y > 0)
{
Console.WriteLine("第一象限");
}
else if (x > 0 && y < 0)
{
Console.WriteLine("第二象限");
}
else if (x < 0 && y > 0)
{
Console.WriteLine("第三象限");
}
else
{
Console.WriteLine("第四象限");
}
switch分支语句
switch(比对的值) 值1 值2都是执行同一个执行语句
{
case 值1:
执行语句1;
break;
case 值2:
执行语句2;
break;
case 值3:
执行语句3;
break;
...
default:
默认执行语句
break;
}
练习 每星期的日程
string day = Console.ReadLine();
switch (day)
{
case "星期一":
case "monday":
Console.WriteLine("都来上课");
break;
case "星期二":
Console.WriteLine("轻松");
break;
case "星期三":
Console.WriteLine("累死了");
break;
case "星期四":
Console.WriteLine("再撑撑");
break;
case "星期五":
Console.WriteLine("要解放了");
break;
}
输入4个整数,求出其中的最大值和最小值,并输出出来
int a = Convert.ToInt32(Console.ReadLine());
int max = a;
int min = a;
int b = Convert.ToInt32(Console.ReadLine());
if (b > max) max = b;
if (b < min) min = b;
int c = Convert.ToInt32(Console.ReadLine());
if (c > max) max = c;
if (c < min) min = c;
int d = Convert.ToInt32(Console.ReadLine());
if (d > max) max = d;
if (d < min) min = d;
Console.WriteLine("最大值是{1}最小值是{0},max,min");