1、异常捕获
try{检测内容}
catch{异常后显示结果}
注意:try和catch之间不能写代码
2、变量的作用域
定义:变量的作用域就是你能够使用到这个变量的范围。
变量的作用域一边从声明他的那个括号开始到那个括号所对应的结束的括号结束。
在这个范围内,我们可以访问并使用变量。超出这个范围就访问不到了。
using System;
namespace _027_异常捕获
{
class Program
{
static void Main(string[] args)
{
//语法上没有错误,在程序运行的过程当中,由于某些原因出现程序错误,不能再正常运行
bool n = true;
Console.WriteLine("请输入一个数字:");
int number = 0;//声明了变量
try
{
number = Convert.ToInt32(Console.ReadLine());//赋值
//Console.WriteLine(number * 2);//使用
}
catch
{
Console.WriteLine("输入的内容不能转换成数字");
n = false;
}
//我们如果要执行下面代码,需要满足某些条件
//让代码满足某些条件去执行的话,我们使用bool类型
if (n)
{
Console.WriteLine(number * 2);//使用
}
Console.ReadKey();
}
}
}
3、switch-case
作用:用来处理多条件的定值的判断
语法:
switch(变量或者表达式的值)
{
case 值1:要执行的代码;
break;
case 值2:要执行的代码;
break;
case 值3:要执行的代码;
break;
.。。。。。。。。
default:要执行的代码;
break;
}
练习:
工资评定案例
if-else用法
if (level == "A")
{
salary += 500;
}
else if (level == "B")
{
salary += 200;
}
else if (level == "C")
{
salary += 0;
}
else if (level == "D")
{
salary -= 200;
}
else if (level == "E")
{
salary -= 500;
}
else
{
Console.WriteLine("输入有误,程序退出");
b = false;
}
if (b)
{
Console.WriteLine("李四来年的工资是{0}", salary);
}
using System;
namespace _016_switch分支语句
{
class Program
{
static void Main(string[] args)
{
bool b = true;
double salary = 5000;
Console.WriteLine("请输入李四的工资评定:");//a b c d e
string level = Console.ReadLine();
switch(level)
{
case "A":salary += 500;
break;
case "B":salary += 200;
break;
case "C":salary += 0;
break;
case "D":salary -= 200;
break;
case "E":salary -= 500;
break;
default: Console.WriteLine("输入有误,程序退出");
b = false;
break;
}
if (b)
{
Console.WriteLine("李斯明年的工资是{0}元", salary);
}
Console.ReadKey();
}
}
}
成绩评级案例
Console.WriteLine("请输入一个考试成绩:");
int score = 0;
bool b = true;
try
{
score = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("你输入的不是考试成绩");
b = false;
}
if (b)
{
switch(score/10)
{
case 10:
case 9:Console.WriteLine("成绩评级是A");
break;
case 8:Console.WriteLine("成绩评级是B");
break;
case 7: Console.WriteLine("成绩评级是C");
break;
case 6: Console.WriteLine("成绩评级是D");
break;
default : Console.WriteLine("成绩评级是E");
break;
}
}
Console.ReadKey();
年份判断是否为闰年
using System;
namespace _028_练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入年份:");
try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份:");
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 27;
}
break;
}
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}//if判断的括号
else
{
Console.WriteLine("输入的月份有误,已退出");
}
}//try月份括号
catch//catch月份
{
Console.WriteLine("输入的月份有误,已退出");
}
}//try年份括号
catch //catch年份
{
Console.WriteLine("输入的年份有误,已退出");
}
Console.ReadKey();
}
}
}
4、循环语句while
while循环:
while(循环语句)
{
循环体;
}
特点:先判断,在执行,有可能一边循环都不执行。
using System;
namespace _018_循环语句while
{
class Program
{
static void Main(String[] arg)
{
//向控制台打印100遍下次考试一定要细心
//循环体Console.WriteLine("下次考试一定要细心\t{0}",n);
//循环条件:打印次数小于100
int n = 1;
while(n<101)
{
Console.WriteLine("下次考试一定要细心\t{0}",n);
n++;
}
}
}
}
4、break
作用:
1)、可以跳出switch-case结构
2)、跳出while循环
交替显示10次
break一般不单独的使用,二十跟着if判断一起使用,表示当满足某些条件的时候,就不再循环。
练习
输入学生id,成绩,计算平均值,总成绩
using System;
namespace _029_练习
{
class Program
{
static void Main(string[] args)
{
//输入班级人数,然后依次输入成绩计算班级的平均成绩和总成绩
int number;
Console.WriteLine("总人数是");
try
{
number = Convert.ToInt32(Console.ReadLine());
int sorce;
int totalSorce = 0;
int i = 1;
Console.WriteLine("每个人的分数是");
try
{
while (i <= number)
{
Console.WriteLine("请输入第{0}个学生的成绩,i");
sorce = Convert.ToInt32(Console.ReadLine());
i++;
totalSorce += sorce;
}
double levelSorce = totalSorce * 1.0 / number;
Console.WriteLine("总成绩是{0}", totalSorce);
Console.WriteLine("平均成绩是{0:0.00}", levelSorce);
} catch
{
Console.WriteLine("请输入正确的分数");
}
} catch
{
Console.WriteLine("请输入正确的人数");
}
Console.ReadKey();
}
}
}
老师放学前询问是否会了,如果不会就继续讲直到10次,就直接放学。每次讲完后会继续询问是否会了,如果学会了就结束放学。
using System;
namespace _029_练习1
{
class Program
{
static void Main(string[] args)
{
string answer = " ";
int i = 1;
while(answer != "yes" && i <= 10)
{
Console.WriteLine("这是我讲的第{0}遍,你们回了吗?yes/no",i);
answer = Console.ReadLine();
if(answer == "yes")
{
Console.WriteLine("会了那就放学!!!");
break;
}
i++;
}
Console.ReadKey();
}
}
}
题目在代码中:
using System;
namespace _030_练习2
{
class Program
{
static void Main(string[] args)
{
//写两个循环
//第一个循环提升用户A输入用户名,要求A的用户名不能为空,只要为空,就会要求A一直重新输入
//循环体:提示A输入用户名 接受 判断
//循环条件:用户名为空
string username1 = "";
while(username1 == "")
{
Console.WriteLine("请输入用户名,不能为空");
username1 = Console.ReadLine();
}
//第二个人提示用户B输入用户名,要求B的用户名不能与A相同并且不能为空
//只要为空或者与A的用户名相同。就会要求B重新输入
//循环体:提示B输入用户名 接受 判断
//循环条件:用户名为空或者与A相同
string username2 = "";
while(username2 == "" || username2 == username1)
{
if(username2 == "")
{
Console.WriteLine("请输入用户名,不能为空");
username2 = Console.ReadLine();
}
else if(username2 == username1)
{
Console.WriteLine("请输入用户名,不能与A相同");
username2 = Console.ReadLine();
}
}
Console.ReadKey();
}
}
}
5、do while
练习:题目在代码中
using System;
namespace _021_do_while循环
{
class Program
{
static void Main(string[] args)
{
//明天小兰要登台演出了,老师说再把明天演出的歌曲在唱一边
//如果满意,小兰就可以回家了。否则就需要在练习一遍,指导老师满意为止
//循环体:小兰唱一遍 问老师 满意吗?老师回答
//循环条件:老师不满意
Console.WriteLine("老师我唱的满意吗?");
string answer = Console.ReadLine();
while(answer == "no")
{
Console.WriteLine("老师,我再唱一遍,你满意了吗?");
answer = Console.ReadLine();
}
//遇到这种首先执行一遍循环体,拿着执行后的结果再去判断是否执行循环
//我们这推荐使用do-while循环
Console.ReadKey();
}
}
}
do
{
循环条件
}while(循环条件);
执行过程::程序首先会执行do中的循环体,执行完成后,去判断do-while循环的循环条件,
如果成立,则继续执行do中的循环体,如果不成立,则跳出do-while循环。
特点:先循环,在判断,最少执行一边循环体。
string answer = "";
do
{
Console.WriteLine("老师,我唱的你满意吗?yes/no");
answer = Console.ReadLine();
} while (answer == "no");
Console.WriteLine("OK,放学回家~~~~");
Console.ReadKey();
string username = "";
string password = "";
do
{
Console.WriteLine("请输入用户名");
username = Console.ReadLine();
Console.WriteLine("请输入密码");
password = Console.ReadLine();
}while(username != "admin" || password != "888888");
Console.WriteLine("登录成功");
Console.ReadKey();
//不断要求用户输入学生姓名,输入q结束
string name = "";
while (name != "q")
{
Console.WriteLine("请输入学生姓名,输入q结束");
name = Console.ReadLine();
}
//do
//{
// Console.WriteLine("请输入学生姓名,输入q结束");
// name = Console.ReadLine();
//} while (name != "q");
Console.ReadKey();