1 数组的声明及赋值方法
方式一
int[] scores = {1,2,3,4,5};//在数组声明的时候赋值
方式二:定义数组的长度,没有赋值,元素默认值为0
int[] scores = new int[10];//数组长度为10,数组元素默认值是0
//也可以这样写
int[] scores;//数组的初始化
scores = new int[10];
方式三:定义数组的长度,有赋值
int[] scores =new int[5]{ 1, 2, 3, 4, 5 };//[]里的值可以不指定
注意!!!当访问一个索引不存在的值的时候,出现空指针异常
int[] scores = {1,2,3,4,5};
Console.WriteLine(scores[10]);//空指针异常报错
下面给一个示例:
static void Main(string[] args)//数组声明和赋值方法
{
//声明
int[] array;
//初始化 new 数据类型[容量]
array = new int[6];
//通过索引读写每个元素
array[0] = 1;//将数据1赋值给数组的第1个元素
array[1] = 2;//将数据2赋值给数组的第2个元素
array[3] = 3;//将数据3赋值给数组的第4个元素
// for (int i = 0; i < 6; i++)
//上句简单写法
for (int i = 0; i < array.Length; i++)
Console.WriteLine(array[i]);
Console.ReadKey();
}
2 几个数组的练习
(1)输入学生成绩并打印
static void Main(string[] args)//在控制台输入学生数并录入成绩并打印成绩
{
float[] scoreArray = CreateScoreArray();
Console.WriteLine("—————以下为学生成绩—————");
for (int i = 0; i < scoreArray.Length; i++)
{
Console.WriteLine(scoreArray[i].ToString());
}
Console.ReadKey();
}
static float[] CreateScoreArray()//在控制台中录入学生成绩
{
Console.WriteLine("请输入学生人数:");
int StudentsCount = int.Parse(Console.ReadLine());
float[] ScoreArray;
ScoreArray = new float[StudentsCount];
for (int i = 0; i < ScoreArray.Length;)
{
Console.WriteLine("请输入第{0}个学生成绩:", i + 1);
float score = float.Parse(Console.ReadLine());
if (score >= 0 && score <= 100)
ScoreArray[i++] = score;
else
Console.WriteLine("输入成绩数据有误!新重新输入");
}
return ScoreArray;
}
(2)返回数组中的最大值,如输入学生的成绩,找出最高成绩
static void Main(string[] args)// 查找数组最大值
{
float[] scoreArray = CreateScoreArray();
Console.WriteLine("—————以下为学生成绩—————");
for (int i = 0; i < scoreArray.Length; i++)
{
Console.WriteLine(scoreArray[i].ToString());
}
float max = GetMax(scoreArray);
Console.WriteLine("成绩最高为:" + max);
Console.ReadKey();
}
private static float GetMax(float[] array)//查找数组的最大值
{
float max = array[0];
for (int i = 1; i < array.Length; i++)
{
if (max < array[i])
max = array[i];
}
return max;
}
(3)数组的其他写法
static void Main(string[] args)//数组的其他写法
{
//初始化+赋值,下面的可以分开写,先声明,再赋值,分两行string[]array01;array01=new string[2]{"a","b"};,示例如下:
string[] array01 = new string[2] { "a", "b" };
//声明+初始化+赋值,不有分开写,示例如下
bool[] array02 = { true, false, false };
//float[] temp = new float[3] { 3,4,6};
//float max = GetMax(temp);
Console.WriteLine(GetMax(new float[3] { 3, 4, 6 }).ToString());
Console.ReadKey();
}
(4)练习根据年月日,计算当天是本年的第几天
static void Main(string[] args)
{
Console.WriteLine("请输入年:");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("请输入月:");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("请输入日:");
int day = int.Parse(Console.ReadLine());
int days = GetDays(year, month, day);
Console.WriteLine("您输入年月日是当年的第{0}天", days);
Console.ReadKey();
}
private static int GetDays(int year, int month, int day)
{
return GetTotalDays(year, month, day);
}
private static int GetTotalDays(int year, int month, int day)
{
int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeapYear(year) ) daysOfMonth[1] = 29;
int days = 0;
for (int i=0;i<(month-1); i++)
{
days+=daysOfMonth[i];
}
return days+day;
}
private static bool IsLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
}