参考书籍:《C#边做边学》;
3.结构化程序设计
3.1 结构化程序设计的3种基本结构
-
顺序结构:先执行 A {\rm A} A语句,再执行 B {\rm B} B语句,两者是顺序执行的关系;
-
选择结构:根据所定选择条件为真与否,决定从各个不同的操作分支中执行某一分支的相应操作,常用的选择结构有条件语句和分支语句;
-
条件语句:
-
i f {\rm if} if语句:基于布尔表达式的值来判定是否执行后面的内嵌语句块;
// if语句语法格式: if(表达式) { 语句块; } // 解释: // 如果表达式值为真,则执行大括号里面的语句块; // 如果表达式值为假,则不执行大括号里面的语句块,再执行程序中的后一条语句;
-
i f − e l s e {\rm if-else} if−else语句:
// if-else语句语法格式: if(表达式) { 语句1; } else { 语句2; } // 解释: // 如果表达式值为真,则执行if后大括号里面的语句块1; // 如果表达式值为假,则执行else后大括号里面的语句块2,再执行程序中的后一条语句;
// if-else语句嵌套: if(表达式1) { if(表达式2) { if(表达式3) { 语句1; } else { 语句2; } } else { 语句3; } } else { 语句4; }
-
e l s e i f {\rm else\ if} else if语句
// else if语句语法格式: if(表达式1) { 语句1; } else if(表达式2) { 语句2; } else if(表达式3) { 语句3; } ... else if(表达式n-1) { 语句n-1; } else { 语句n; }
-
-
分支语句:
// 分支语句switch语法格式: switch(表达式) { case 常量表达式1: 语句1; break; case 常量表达式2: 语句2; break; ... case 常量表达式n; 语句n; break; default: 语句n+1; break; } // 解释: // 1.计算switch后表达式的值; // 2.若case后的常量表达式等于switch后表达式的值,则执行该case后的语句; // 3.若switch后表达式的值与任何一个case后的常量表达式的值都不等,则执行default后的语句;
-
标签语句:
// 标签语句用于配合goto语句完成程序的跳转功能,语法格式: // 标签名称:语句 if (x>0) { goto large; x = -x; } large:return x;
3.2 基本结构示例
3.2.1 if相关结构示例
// CH03_01.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_01
{
class CH03_01
{
static void Main(string[] args)
{
// 定义一个整数变量并赋值
int iNumber = 10;
// 使用if结构进行条件判断
if (iNumber > 0)
{
Console.WriteLine("此数字大于0.");
}
else if (iNumber < 0)
{
Console.WriteLine("此数字小于0.");
}
else
{
Console.WriteLine("此数字等于0.");
}
// 使用嵌套if结构:检查一个数字是正数且大于5;
if (iNumber > 0)
{
if (iNumber > 5)
{
Console.WriteLine("此数字是正数,且大于5.");
}
else
{
Console.WriteLine("此数字是正数,但不大于5.");
}
}
// 等待用户输入以避免程序立即退出
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
3.2.2 switch结构示例
// CH03_02.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_02
{
class CH03_02
{
static void Main(string[] args)
{
// 定义一个整数变量并赋值
int day = 3;
// 使用switch结构进行条件判断
switch (day)
{
case 1:
Console.WriteLine("Today is Monday.");
break;
case 2:
Console.WriteLine("Today is Tuesday.");
break;
case 3:
Console.WriteLine("Today is Wednesday.");
break;
case 4:
Console.WriteLine("Today is Thursday.");
break;
case 5:
Console.WriteLine("Today is Friday.");
break;
case 6:
Console.WriteLine("Today is Saturday.");
break;
case 7:
Console.WriteLine("Today is Sunday.");
break;
default:
Console.WriteLine("Invalid day.");
break;
}
// 等待用户输入以避免程序立即退出
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
// CH03_03.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_03
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("AGV控制指令(W:前进,A:左转,X:后退,D:右转,S:制动)");
Console.WriteLine("请输入AGV控制指令:");
char inputChar = Console.ReadKey().KeyChar;
char processedChar = (char.IsControl(inputChar) || char.IsWhiteSpace(inputChar)) ? '\0' : char.ToUpper(inputChar);
switch (processedChar)
{
case 'W':
Console.Write("\n");
Console.WriteLine("AGV正在向前运动...");
break;
case 'A':
Console.Write("\n");
Console.WriteLine("AGV正在进行左转...");
break;
case 'X':
Console.Write("\n");
Console.WriteLine("AGV正在向后运动...");
break;
case 'D':
Console.Write("\n");
Console.WriteLine("AGV正在进行右转...");
break;
case 'S':
Console.Write("\n");
Console.WriteLine("AGV正在进行制动...");
break;
}
Console.WriteLine("Press any key to close the program...");
Console.ReadKey();
}
}
}
3.3 项目实战
项目需求: 设计一个程序,用于实现学生成绩的统计。
功能分析:
- 依次接收班级每个学生的成绩;
- 每接收一个成绩,依据要求进行统计汇总;
程序流程图分析:
代码实现:
// CH03_04.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_04
{
class CH03_04
{
static void Main(string[] args)
{
/*
* 定义变量:
* iStudents:用于统计学生人数;
* tempScore:接收每次输入成绩;
* maxScore、minScore:最大成绩和最小成绩;
*/
int iStudents = 0;
int tempScore;
int maxScore = 0, minScore = 0;
// 统计各成绩区间内的人数;
int score0To59 = 0, score60To69 = 0, score70To79 = 0, score80To89 = 0, score90To100 = 0;
Console.WriteLine("--------学生成绩统计----------");
Console.WriteLine("统计要求:学生成绩正确的区间为0~100,\n" + " 如果输入的成绩不在此区间中,可以选择退出统计!");
Console.WriteLine();
loop: Console.WriteLine("请输入学生成绩:");
string strScore = Console.ReadLine();
if (strScore == null)
{
return;
}
tempScore = int.Parse(strScore);
if ((tempScore < 0) || (tempScore > 100))
{
Console.WriteLine("你输入的分数不对,选择是(Y)退出还是继续(N)");
string strSelect = Console.ReadLine();
if (strSelect == "N")
{
goto loop;
}
else
{
// 输出统计信息;
Console.WriteLine("--------学生成绩统计信息输出----------");
Console.WriteLine("全班共{0:d}人,其中最高成绩{1:f2}," + "最低成绩{2:f2}", iStudents, maxScore, minScore);
Console.WriteLine("成绩区间90~100的人数有{0:d}人," + "所占比例为:{1:f2}%", score90To100, score90To100 * 100 / iStudents);
Console.WriteLine("成绩区间80~89的人数有{0:d}人," + "所占比例为:{1:f2}%", score80To89, score80To89 * 100 / iStudents);
Console.WriteLine("成绩区间70~79的人数有{0:d}人," + "所占比例为:{1:f2}%", score70To79, score70To79 * 100 / iStudents);
Console.WriteLine("成绩区间60~69的人数有{0:d}人," + "所占比例为:{1:f2}%", score60To69, score60To69 * 100 / iStudents);
Console.WriteLine("成绩区间0~59的人数有{0:d}人," + "所占比例为:{1:f2}%", score0To59, score0To59 * 100 / iStudents);
Console.ReadLine();
}
}
else
{
// 统计学生人数;
iStudents++;
if (iStudents == 1)
{
maxScore = tempScore;
minScore = tempScore;
}
else
{
// 修正最大与最小成绩;
if (maxScore < tempScore)
{
maxScore = tempScore;
}
if (minScore > tempScore)
{
minScore = tempScore;
}
}
// 接收成绩所在区间的人数;
int temp = tempScore / 10;
switch (temp)
{
case 10:
case 9:
Console.WriteLine("成绩优秀");
score90To100++;
break;
case 8:
Console.WriteLine("成绩良好");
score80To89++;
break;
case 7:
Console.WriteLine("成绩中等");
score70To79++;
break;
case 6:
Console.WriteLine("成绩及格");
score60To69++;
break;
default:
Console.WriteLine("成绩不及格");
score0To59++;
break;
}
}
goto loop;
}
}
}
3.4 循环结构
-
循环结构:指在程序中从某处开始有规律地反复执行某一语句块的结构,把重复执行的语句块称为循环体;
-
w h i l e {\rm while} while语句语法:
// while语句语法格式: while(条件表达式) { 循环体; } // 解释: // 先判断条件表达式,如果值为true,则执行循环体;如果值为false,则不执行循环体;
-
d o − w h i l e {\rm do-while} do−while语句语法:
// do-while语句语法格式: do { 循环体; } while(条件表达式); // 解释: // 先执行循环体,再对while语句后条件表达式判定,若值为true,则继续执行循环体;若为false,则退出循环;
-
f o r {\rm for} for语句语法:
// for语句语法格式: for(表达式1;表达式2;表达式3) { 循环体; } // 解释: // 1.计算表达式1的值; // 2.判定表达式2的值,若值为true,执行循环体的语句,求表达式3的值;若值为false,执行程序下一条语句;
-
f o r e a c h {\rm foreach} foreach语句语法:
// foreach基础: // 1.foreach语句用于列举集合中的每一个元素,且通过执行循环体对每一个元素进行操作; // 2.foreach语句只能对集合中的元素进行循环操作; // foreach语句语法格式: foreach(数据类型 标识符 in 表达式) { 循环体; }
3.5 循环结构基本示例
// CH03_06.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_05
{
class CH03_05
{
static void Main(string[] args)
{
int sum = 0; // 用于存储总和的变量
int number; // 用于存储用户输入的整数
Console.WriteLine("请输入一个整数(输入负数停止):");
// 使用while循环读取用户输入并计算总和
while (true)
{
// 读取用户输入;
string input = Console.ReadLine();
// 尝试将输入转换为整数;
bool isNumber = int.TryParse(input, out number);
if (!isNumber) // 如果输入不是整数
{
Console.WriteLine("输入无效,请输入一个整数。");
}
else if (number < 0) // 如果输入的是负数
{
break; // 退出循环
}
else
{
// 如果输入的是正整数或零,则将其加到总和中
sum += number;
Console.WriteLine("当前总和为:" + sum);
}
}
// 循环结束后,输出最终的总和
Console.WriteLine("最终总和为:" + sum);
}
}
}
// CH03_06.cs
// 需求:用户输入一个数字,并且只有在输入的数字是正数时才会停止循环;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_06
{
class CH03_06
{
static void Main(string[] args)
{
int number;
string input;
do
{
Console.Write("请输入一个正数: ");
input = Console.ReadLine();
// 检查输入是否为有效的整数且为正数
if (!int.TryParse(input, out number) || number <= 0)
{
Console.WriteLine("输入无效,请输入一个正整数。");
}
} while (!int.TryParse(input, out number) || number <= 0);
// 循环结束,输出用户输入的正数
Console.WriteLine("您输入的正数是: " + number);
}
}
}
// CH03_07.cs
// 需求:从键盘输入一个数字,并使用for循环结构计算从1到这个数字的和;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_07
{
class CH03_07
{
static void Main(string[] args)
{
int number;
int sum = 0;
// 提示用户输入一个数字
Console.Write("请输入一个数字: ");
// 尝试从键盘读取输入并转换为整数
if (int.TryParse(Console.ReadLine(), out number))
{
// 使用for循环计算从1到输入数字的和
for (int i = 1; i <= number; i++)
{
sum += i;
}
// 输出结果
Console.WriteLine("从1到{0}的和是: {1}", number, sum);
}
else
{
// 如果输入的不是有效的整数,输出错误信息
Console.WriteLine("输入无效,请输入一个整数。");
}
}
}
}
// CH03_08.cs
// 需求:遍历数组元素并输出,计算数组元素的和并输出;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_08
{
class CH03_08
{
static void Main(string[] args)
{
// 创建一个整数数组
int[] numbers = { 10, 25, 35, 40, 55 };
// 用于存储总和的变量
int sum = 0;
// 使用foreach循环遍历数组中的每个元素
foreach (int number in numbers)
{
Console.WriteLine("此时数组的值为:" + number);
sum += number;
}
// 输出总和
Console.WriteLine("数组中所有元素的和是: " + sum);
}
}
}
3.6 项目实战
项目需求:实现一个用于学生成绩统计的程序。
功能分析:
- 接收全班人数;
- 依次接收班级每个学生的成绩;
- 每接收一个成绩,依据要求进行统计汇总;
程序流程图分析:
代码实现:
// CH03_09.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03_09
{
class CH03_09
{
private static int iStudents;
static void Main(string[] args)
{
/*
* 参数含义:
* nStudents:用于统计学生人数;
* tempScore:接收每次输入的成绩;
* maxScore,minScore:成绩最大值、成绩最小值;
*/
int nStudents;
int tempScore;
int maxScore = 0, minScore = 0;
int score0To59 = 0, score60To69 = 0, score70To79 = 0, score80To89 = 0, score90To100 = 0;
Console.WriteLine("--------学生成绩统计--------");
Console.WriteLine("请输入班级人数:nStudents = ");
string s = Console.ReadLine();
nStudents = Int32.Parse(s);
// 接收并处理学生成绩;
for(int i = 1; i <= nStudents; i++)
{
Console.WriteLine("请输入第{0:d}个学生的成绩", i);
tempScore = Int32.Parse(Console.ReadLine());
if (i == 1)
{
maxScore = tempScore;
minScore = tempScore;
}
else // 最大成绩和最小成绩;
{
if (maxScore < tempScore)
{
maxScore = tempScore;
}
if (minScore > tempScore)
{
minScore = tempScore;
}
}
int temp = tempScore / 10;
// 统计各区间成绩的人数;
switch (temp)
{
case 10:
case 9:
Console.WriteLine("成绩优秀");
score90To100++;
break;
case 8:
Console.WriteLine("成绩良好");
score80To89++;
break;
case 7:
Console.WriteLine("成绩中等");
score70To79++;
break;
case 6:
Console.WriteLine("成绩及格");
score60To69++;
break;
default:
Console.WriteLine("成绩不及格");
score0To59++;
break;
}
}
// 输出统计信息;
Console.WriteLine("--------学生成绩统计信息输出----------");
Console.WriteLine("全班共{0:d}人,其中最高成绩{1:f2}," + "最低成绩{2:f2}", nStudents, maxScore, minScore);
Console.WriteLine("成绩区间90~100的人数有{0:d}人," + "所占比例为:{1:f2}%", score90To100, score90To100 * 100 / nStudents);
Console.WriteLine("成绩区间80~89的人数有{0:d}人," + "所占比例为:{1:f2}%", score80To89, score80To89 * 100 / nStudents);
Console.WriteLine("成绩区间70~79的人数有{0:d}人," + "所占比例为:{1:f2}%", score70To79, score70To79 * 100 / nStudents);
Console.WriteLine("成绩区间60~69的人数有{0:d}人," + "所占比例为:{1:f2}%", score60To69, score60To69 * 100 / nStudents);
Console.WriteLine("成绩区间0~59的人数有{0:d}人," + "所占比例为:{1:f2}%", score0To59, score0To59 * 100 / nStudents);
Console.ReadLine();
}
}
}