顺序语句
顺序结构是程序代码中最基本的结构,是指程序中的所有语句都是按书写顺序逐一执行的,代码从main()函数开始运行,从上到下,一行一行地执行,不漏掉代码。
分支选择语句
if条件语句
一个if语句的条件可以是布尔变量,也可以是表达式,但如果是表达式,则得到的结果必须是布尔值。
- 单选择if语句
使用if语句,判断一个变量是否符合条件。 - if…else语句
使用if…else语句,判断一个变量的大小。 - if…else if…else 语句
输入一位同学的成绩,并输出相应的评价。
using System;
namespace Project4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入学生成绩:");
string s = Console.ReadLine();
int a;
a = int.Parse(s); //将字符串转换为int型数据
/* 检查布尔条件 */
if (a >= 90)
{
/* 如果 if 条件为真,则输出下面的语句 */
Console.WriteLine("成绩优秀");
}
else if (a >= 80)
{
/* 如果 else if 条件为真,则输出下面的语句 */
Console.WriteLine("成绩优良");
}
else if (a >= 70)
{
/* 如果 else if 条件为真,则输出下面的语句 */
Console.WriteLine("成绩中等");
}
else if (a >= 60)
{
/* 如果 else if 条件为真,则输出下面的语句 */
Console.WriteLine("再接再厉");
}
else
{
/* 如果上面条件都不为真,则输出下面的语句 */
Console.WriteLine("继续努力");
}
Console.ReadKey();
}
}
}
【程序分析】本例演示了if…else if…else语句的使用。在代码中首先定义一个string类型的变量s;然后输入一个学生的成绩赋给该变量,并通过Parse()方法,将变量s转换成int类型的数据赋给变量a;最后对a进行判断,输出对应的评价语句。
请输入学生成绩:
88
成绩优良
- 嵌套if语句
编写程序,使用嵌套的if语句,计算出打折后的机票价格。
using System;
namespace Project5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("********************************************");
Console.WriteLine("* 机票预定 *");
Console.WriteLine("* 注意事项: *");
Console.WriteLine("* 5-10月为旺季,头等舱打9折,经济舱打7.5折 *");
Console.WriteLine("* 其他时间为淡季,头等舱打6折,经济舱打3折 *");
Console.WriteLine("* *");
Console.WriteLine("********************************************");
Console.WriteLine("请输入机票原价:");
string str1 = Console.ReadLine();
double money = double.Parse(str1);
Console.WriteLine("请输入月份:");
string str2 = Console.ReadLine();
int month = int.Parse(str2);
Console.WriteLine("选择仓位:1.头等舱 2.经济舱");
string str3 = Console.ReadLine();
int type = int.Parse(str3);
if (month >= 5 && month <= 10) //旺季
{
if (type == 1) //头等舱
{
Console.WriteLine("您的机票价格为:{0}", money * 0.9);
}
else if (type == 2) //经济舱
{
Console.WriteLine("您的机票价格为:{0}", money * 0.75);
}
}
else
{
if (type == 1) //头等舱
{
Console.WriteLine("您的机票价格为:{0}", money * 0.6);
}
else if (type == 2) //经济舱
{
Console.WriteLine("您的机票价格为:{0}", money * 0.3);
}
}
Console.ReadKey();
}
}
}
【程序分析】本例演示了嵌套if语句的使用。在代码中,定义了三个变量money、month、type,分别用于表示机票价格、月份和飞机仓位;然后通过嵌套的if语句,计算出某个月份预订机票的打折价格。
********************************************
* 机票预定 *
* 注意事项: *
* 5-10月为旺季,头等舱打9折,经济舱打7.5折 *
* 其他时间为淡季,头等舱打6折,经济舱打3折 *
* *
********************************************
请输入机票原价:
1000
请输入月份:
6
选择仓位:1.头等舱 2.经济舱
2
您的机票价格为:750
switch语句
一个switch语句允许测试一个变量等于多个值时的情况。每个值称为一个case,且被测试的变量会对每个case进行检查。
using System;
namespace Project5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字:");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入运算类型:");
char z = char.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数字:");
int y = int.Parse(Console.ReadLine());
switch (z)
{
case '+':
Console.WriteLine("计算结果为:{0}", x + y);
Console.ReadKey();
break;
case '-':
Console.WriteLine("计算结果为:{0}", x - y);
Console.ReadKey();
break;
case '*':
Console.WriteLine("计算结果为:{0}", x * y);
Console.ReadKey();
break;
case '/':
Console.WriteLine("计算结果为:{0}", x / y);
Console.ReadKey();
break;
default:
Console.WriteLine("运算类型输入错误!");
Console.ReadKey();
break;
}
Console.ReadKey();
}
}
}
请输入第一个数字:
5
请输入运算类型:
*
请输入第二个数字:
5
计算结果为:25
用户可以把一个switch语句嵌套在另一个switch语句内。即使内部和外部switch的case常量包含共同的值,也是没有矛盾的。