哈喽,大家好。又到了总结知识的时间了。今天在云和学院自学了一下循环语句,下面是自己总的一些知识点。
先补充一下选择结构中的switch语句。
理论:switch语句是一种多分支选择语句,当需要测试大量选择项时,可以使用switch语句。switch结构可以用于代替多条选择路径的if语句。
形式是:switch(表达式)
{
case 常量表达式1:
语句1;
break;
case 常量表达式2:
语句2;
break;
.
.
case 常量表达式n:
语句n;
break;
default;
语句n+1;
break;
}
注意:表达式的类型可以是整数类型和字符串类型,而各个case后的常量表达式类型必须与表达式的类型相同或能够隐式地转换为表达式的类型。各个case后的常量表达式不能相等;每个case分支都必须以break语句、return语句、goto语句或throw语句结束;而且语句中的任何代码都不能修改switch后面表达式的值。
循环语句——while
while(表达式)
{
语句;
}
实操题:
李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,则工资涨200元,如果定为C级,工资不变,如果定为D级工资降200元,如果定为E级工资降500元.设李四的原工资为5000,请用户输入李四的评级,然后显示李四来年的工资.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
Console.WriteLine(
"请输入李四的评级"
);
string
dengji = Console.ReadLine();
decimal
money = 5000;
decimal
moneys = 0;
switch
(dengji)
{
case
"A"
:
moneys = money + 500;
break
;
case
"B"
:
moneys = money + 200;
break
;
case
"C"
:
moneys = money;
break
;
case
"D"
:
moneys = money - 200;
break
;
case
"E"
:
moneys = money - 500;
break
;
}
Console.WriteLine(
"李四来年的工资为:{0}"
,moneys);
Console.ReadKey();<br><strong><span style=
"font-size: 18px;"
>李四这次考试又粗心了,爸爸让他写1000遍“下次考试一定要细心”.</span></strong>
|
1
2
3
4
5
6
7
8
|
int
i = 0;
while
(i <= 1000)
{
i++;
Console.WriteLine(
"下次考试一定要细心"
);
}
Console.ReadKey();
|
请用户输年份,再输入月份,输出该月的天数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
Console.WriteLine(
"请输入年份"
);
string
year = Console.ReadLine();
int
years=Convert .ToInt32(year );
Console.WriteLine(
"请输入月份"
);
string
month = Console.ReadLine();
int
month1 = Convert.ToInt32(month);
if
(month ==
"2"
)
{
if
(years % 400 == 0 || years % 4 == 0 && years % 100 == 0)
{
Console.WriteLine(
"这个月有29天"
);
}
else
{
Console.WriteLine(
"这个月有28天"
);
}
}
else
if
(month1 >=1 && month1 <= 12 && month1!=2)
{
switch
(month)
{
case
"1"
:
case
"3"
:
case
"5"
:
case
"7"
:
case
"8"
:
case
"10"
:
case
"12"
:
Console.WriteLine(
"这个月有31天"
);
break
;
default
:
Console.WriteLine(
"这个月有30天"
);
break
;
}
}
else
{
Console.WriteLine(
"输入错误,请重新输入"
);
}
Console.ReadKey();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
int
a = 1;
Console.WriteLine(
"老师问学生,这道题你会做了吗? y/n"
);
string
stu1 = Console.ReadLine();
if
(stu1 ==
"n"
)
{
do
{
a++;
Console.WriteLine(
"老师问学生,这道题你会做了吗? y/n"
);
}
while
(a <= 10);
Console.WriteLine(
"放学"
);
}
else
{
Console.WriteLine(
"可以放学"
);
}
Console.ReadKey();
|
当我输入”n"时,运行结果是: