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
}
}