1 C#中的数组
1.1 语法
数据类型[] 数组名;
数组声明同时初始化的三种情况:
int[] netScore1 = new int[3] { 67, 89, 78 };
int[] netScore2 = new int[] { 67, 89, 78 };
int[] netScore3 = { 67, 89, 78 };
1.2 使用foreach遍历数组
foreach循环结构:
foreach (元素类型 变量名 in 集合或者数组名)
{
//语句
}
static void Test()
{
int[] netScore = new int[] { 67, 89, 78, 69, 95 };
int sumScore = 0;
//使用for循环遍历数组
for (int i = 0; i < netScore.Length; i++)
{
sumScore += netScore[i];
}
int avgScore = sumScore / netScore.Length;
Console.WriteLine($"学员的平均成绩:{avgScore}");
}
参考资料:
580

被折叠的 条评论
为什么被折叠?



