文章目录
前言:此专题是为刚学习C#入门语法的新手,使用情景模块进行学习。
参考资料:【01_C#入门到精通】新手强烈推荐:C#开发课程,一整套课程
1.异常捕获
格式
//语法格式
try
{
}
catch
{
}
实例:转换数字
//异常捕获
static void Main(string[] args)
{
int number=0;
Console.WriteLine("请输入一个数字");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的内容不能够转换成数字");
}
Console.WriteLine(number*2);
Console.ReadKey();
}
2.switch-case语句
格式:
switch(表达式)
{case 值 1:
语句块 1;
break;
case 值 2:
语句块 2;
break;
...
default:
语句块 n;
break;
}
实例1:判断成绩考试等级
// switch case语句
1.判断成绩考试等级
static void Main(string[] args)
{
Console.WriteLine("请输入一个考试成绩");
int score = Convert.ToInt32(Console.ReadLine());
switch (score / 10)
{
case 10:
Console.WriteLine("A");
break;
case 9:
Console.WriteLine("B");
break;
case 8:
Console.WriteLine("C");
break;
case 7:
Console.WriteLine("D");
break;
default:
Console.WriteLine("E");
break;
}
Console.ReadKey();
实例2:判断年份月数
2.判断年份月数
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入一个年份");
int year = Convert.ToInt32(Console.ReadLine());
try
{
Console.WriteLine("请输入一个月份");
int month = Convert.ToInt32(Console.ReadLine());
if ((month >= 1) && (month <= 12))
{
int day;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 29;
}
else
{
day = 28;
}
break;
default:
day = 30;
break;
}
Console.WriteLine("{0}年{1}月{2}天", year, month, day);
Console.ReadKey();
}
else
{
Console.WriteLine("输入月份数据有误");
}
}
catch
{
Console.WriteLine("输入月份数据有误");
}
}
catch
{
Console.WriteLine("输入年份数据有误");
}
}
3. while
格式
while(表达式)
{
执行语块;
}
实例1:计算班级分数平均值
//while的用法
1.计算班级分数平均值
static void Main(string[] args)
{
Console.WriteLine("请输入班级人数");
int count=Convert.ToInt32(Console.ReadLine());
int i = 1;
int sum = 0;
while(i<=count)
{
Console.WriteLine("请输入第{0}个学员的考试成绩",i);
int score = Convert.ToInt32(Console.ReadLine());
sum += score;
i++;
}
Console.WriteLine("{0}个学生,总分是{1},平均分是{2}", count,sum,sum/count);
Console.ReadKey();
}
实例2:打印数字
2.打印数字
static void Main(string[] args)
{
string input = "";
while(input!="q")
{
Console.WriteLine("请输入一个数字,我们将打印这个数字的2倍");
input = Console.ReadLine();
if (input != "q")
{
try
{
int number = Convert.ToInt32(input);
Console.WriteLine("您输入的数字的2倍是{0}", number * 2);
}
catch
{
Console.WriteLine("您输入的数字有误");
}
}
else
{
Console.WriteLine("输入的是q,程序退出");
}
}
Console.ReadKey();
}
实例3: 输出数字最大值
3.输出数字最大值
static void Main(string[] args)
{
string input = "";
int max = 0;
while (input != "end")
{
try
{
Console.WriteLine("请输入一个数字,输入end我们将会显示你的最大数字");
input = Console.ReadLine();
if (input != "end")
{
int number = Convert.ToInt32(input);
if (number > max)
{
max = number;
}
}
else
{
Console.WriteLine("您刚才输入数字中的的最大值为{0}", max);
}
}
catch
{
Console.WriteLine("您输入的数字有问题");
}
}
Console.ReadKey();
}
4.for循环
格式
for(初始化语句;判断条件语句;控制条件语句)
{
循环体语句;
}
实例
//for循环
水仙花数
static void Main(string[] args)
{
for (int i = 100; i <= 999; i++)
{
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
{
Console.WriteLine("水仙花数有{0}", i);
}
}
Console.ReadKey();
}