第十八节 数组(一)
数组:具有相同类型的一组数据叫做数组。
int [] scores = {};
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShuZu
{
class Program
{
static void Main(string[] args)
{
int[] scores = { 1,3,5,7,9};
scores[0] = 2;
int sum = scores[0] + scores[1] + scores[2] + scores[3] + scores[4];
Console.WriteLine(sum);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShuZu
{
class Program
{
static void Main(string[] args)
{
int[] scores = { 1,3,5,7,9};
int sum=0;
for (int i=0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]);
sum=sum+scores[i];
}
Console.WriteLine("和为"+sum);
float average = sum / scores.Length;
Console.WriteLine("平均数为"+average);
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShuZu
{
class Program
{
static void Main(string[] args)
{
string[] shu = new string[3] {"张三","李四","王五"};
Console.WriteLine("我有[0]位朋友", shu.Length);
for (int i = 0; i < shu.Length; i++)
{
Console.WriteLine(shu[i]);
}
}
}
}
foreach语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShuZu
{
class Program
{
static void Main(string[] args)
{
string[] shu = new string[3] {"张三","李四","王五"};
foreach(string name in shu)
{
Console.WriteLine(name);
}
}
}
}
浙公网安备 33010602011771号