using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//2014.3.10
namespace _31.数组
{
class Program
{
/// <summary>
/// 数组;一次声明多个同类型的变量,这些变量在内存中是连续存储的。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
#region 输出每个学生的成绩和平均成绩
int sum=0;
int max=0;
int[] score = new int[10];
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("请输入第{0}个学生的成绩:",i+1);
score[i]= Convert.ToInt32(Console.ReadLine());
sum += score[i];
if (score[i] > max) //从头遍历用户输入的数组,将大的值赋值给max,一直更新max的值,直到循环结束。
{
max = score[i];
}
}
Console.Clear(); //清屏 因为上一个for循环已经输出了十次“请输入第{0}个学生的成绩:”,看着很乱,清屏之后,对于结果的查看非常直观。
Console.WriteLine("平均成绩为:{0}", sum / score.Length);
Console.WriteLine("最高分为:{0}", max);
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine("第{0}个学生的成绩是{1}",i+1,score[i]);
}
Console.ReadKey();
#endregion
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 数组
{
class Program
{
static void Main(string[] args)
{
#region 初始化数组语法
//这个就是初始化数组的三种语法。
//使用new关键字
string[] strArray = new string[] {"a", "b", "c" };
Console.WriteLine("strArray有{0}个元素", strArray.Length);
//不使用new关键字
bool[] boolArray = { false, false, true };
Console.WriteLine("boolArray有{0}个元素",boolArray.Length);
//使用new关键字和大小
int[] intArray = new int[4] {1,2,3,4 };
Console.WriteLine("intArray有{0}个元素", intArray.Length);
#endregion
#region 矩形数组
//需要注意的是,如果创建了数组,但是没有赋值,那么数组的默认值为0
Console.WriteLine("矩形数组:");
int[,] juxing = new int[6, 6];
//填充矩形数组
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
juxing[i, j] = i * j;
//输出矩形数组
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
Console.Write(juxing[i, j]+"\t");
Console.WriteLine();
}
#endregion
#region 交错数组
Console.WriteLine("交错数组:");
//交错就是数组的数组,声明一个具有5个不同数组的数组。
int[][] jiaocuo = new int[5][];
//创建交错数组
for (int i = 0; i < jiaocuo.Length; i++)
jiaocuo[i] = new int[i + 7];
//输出每一行
for(int i =0;i<5;i++)
{
for (int j = 0; j < jiaocuo[i].Length; j++)
Console.Write(jiaocuo[i][j] + " ");
Console.WriteLine();
}
#endregion
#region 已知数组的值,输出
int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; 这个数组其实就是 1 2
for (int i = 0; i < 3; i++) 3 4
{ 5 6
for (int j = 0; j < 2; j++) 这里定义的i就相当于行 j相当于列 把数组画出来,特别直观,也更容易理解代码
Console.Write(numbers[i, j] + " ");
Console.WriteLine();
}
#endregion
Console.ReadLine();
}
}
}