news 2026/4/25 5:14:38

3] 数组遍历:使用foreach循环实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
3] 数组遍历:使用foreach循环实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _9.数组使用
{
internal class Program
{
static void Main(string[] args)
{

//Test1();
//Test2();
//Test3();
//Test4();
//Test5();
//Test6();
Test7();

string a = "1";
String b = "1";

Console.ReadLine();
}

#region [1] 数组Array


static void Test1()
{

int[] scores; // [1] 声明数组
scores = new int[5]; // [2]分配空间


double[] scores2 = new double[5];
//[3]元素赋值
scores[0] = 67;
scores[1] = 77;
scores[2] = 87;
scores[3] = 97;
scores[4] = 107;

//[4] 元素操作
int avgScor = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) / 5;

Console.WriteLine("数组平均值:" + avgScor);


}


//数组的声明
static void Test2()
{

int[] netscore1 = new int[2] { 1, 2 }; //int[]方括号内数字决定数组的长度
int[] netscore2 = new int[] { 26, 27, 28 };// 数组的元素直接决定数组的长度

int[] netscore3 = { 26, 27, 28 };
}


#endregion

#region [2] for循环实现 数组的遍历

static void Test3()
{
int[] scoreArray = { 67, 89, 78, 80, 75 };

int sumScore = 0;

for (int i = 0; i < scoreArray.Length; i++)
{
sumScore += scoreArray[i];
}

//计算出平均值
int avgScore = sumScore / scoreArray.Length;

Console.WriteLine($"for 循环得到学员的平均成绩:{avgScore}");

}

#endregion

#region [3] 数组遍历:使用foreach循环实现
static void Test4()
{


int[] scoreArray = { 29, 39, 39, 49, 40 };
int sumScore = 0;
//foreach 循环语法结构:(元素类型 变量名 in 数组名/集合名称)

foreach (var score in scoreArray) // 遍历数组每个元素
{
sumScore += score;
}

int avgScore = sumScore / scoreArray.Length;

Console.WriteLine($"foreach 循环得到学员的平均成绩:{avgScore}");
}

#endregion

#region [4] 字符串的分割与拼接、替换
static void Test5()
{
string data1 = "AB EF HU OO";

string[] stringArray = data1.Split(); //分割字符串,返回字符串数组
string data2 = string.Join("-", stringArray); //组合字符串
Console.WriteLine($"第一次拼接{data2}");

Console.WriteLine("--------------");

//使用都好分割, 然后使用&重组和
data1 = "AB,EF,HU,OO";
stringArray = data1.Split(',');
data2 = string.Join("&", stringArray);
Console.WriteLine($"第二次拼接{data2}元素个数 " + stringArray.Length);

Console.WriteLine("--------------");
Console.WriteLine($"字符串替换前输出 " + data1);

//字符串替换
// .Replace() 不会修改原字符串,而是生成并返回一个全新的字符串
string data3 = data1.Replace(',','-');

Console.WriteLine($"字符串替换后输出 "+ data3);
}

#endregion

#region [5] 值类型变量和引用类型变量
static void Test6()
{
//值类型
float tempA = 25.6f;
float tempB = 25.7f;
Console.WriteLine("tempA=" + tempA); //25.6
Console.WriteLine("tempA=" + tempB); //25.7
Console.WriteLine("----------------------------");

float tempC = tempB;
tempC += 0.5f;
Console.WriteLine("tempC= " + tempC); // 27,2
Console.WriteLine("----------------------------");

Console.WriteLine("tempA="+tempA);//25.6 原数据没有发生变化
Console.WriteLine("tempB="+tempB);//25.7
}

static void Test7()
{
float[] tempArray = { 25.6f, 26.7f };

Console.WriteLine("tempA= " + tempArray[0]); // 25.6
Console.WriteLine("tempB= " + tempArray[1]); // 26.7
Console.WriteLine("----------------------");

float[] tempOther = tempArray; // 讲数组赋值给另一个新的数组

tempOther[0] += 0.5f;

Console.WriteLine("tempC= " + tempOther[0]); // 26.1
Console.WriteLine("----------------------");


Console.WriteLine("tempA= " + tempArray[0]); // 26.1
Console.WriteLine("tempB= " + tempArray[1]); // 26.7 原数据发生变化


}


#endregion

}
}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/25 5:14:16

VTCode:基于VS Code核心的轻量级代码编辑器定制方案

1. 项目概述&#xff1a;一个为特定场景优化的代码编辑器如果你和我一样&#xff0c;长期在嵌入式开发、工业控制或者对资源有严格限制的边缘计算环境中工作&#xff0c;那么你肯定对市面上那些“巨无霸”级别的代码编辑器又爱又恨。它们功能强大&#xff0c;但启动慢、占用高&…

作者头像 李华
网站建设 2026/4/25 5:14:05

别再只用defaultToolbar了!解锁Layui Table三大核心功能的独立调用秘籍

解锁Layui Table高级玩法&#xff1a;独立调用三大核心功能的工程化实践 每次看到项目中满屏重复的defaultToolbar:[filter,exports,print]配置&#xff0c;总有种说不出的别扭感。在复杂后台系统中&#xff0c;这种写法不仅导致代码冗余&#xff0c;更让功能复用变得困难。今天…

作者头像 李华
网站建设 2026/4/25 5:13:40

从软件测试到Web3漏洞猎人:专业转型与技术跃迁之路

新纪元的职业新边疆对于长期与软件质量、功能边界打交道的测试工程师而言&#xff0c;一个融合了尖端技术与高价值回报的领域正展现出前所未有的吸引力——Web3安全&#xff0c;尤其是其中的漏洞猎人&#xff08;Bug Bounty Hunter&#xff09;角色。这不仅是职业赛道的转换&am…

作者头像 李华
网站建设 2026/4/25 5:13:29

SpringBoot项目迁移到国产东方通TongWeb7.0的完整避坑指南(附依赖清单)

SpringBoot项目迁移到国产东方通TongWeb7.0的完整避坑指南&#xff08;附依赖清单&#xff09; 在国产化技术替代浪潮中&#xff0c;应用服务器的迁移是许多Java开发者必须面对的挑战。本文将分享从Tomcat迁移到东方通TongWeb7.0的完整过程&#xff0c;重点解决实际迁移中遇到的…

作者头像 李华