不用下标遍历一维数组;
private void button1_Click(object sender, EventArgs e)
{
double[] points = { 80.2, 88.1, 86.3, 90, 75.5 };
double sum = 0;
double avg = 0;
foreach (double point in points)
{
sum = sum + point;
}
avg = sum / points.Length;
textBox1.Text = "总成绩:"+ sum.ToString() + Environment.NewLine + "平均成绩:"+ avg.ToString();
}
不用嵌套循环遍历二维数组;
private void button2_Click(object sender, EventArgs e)
{
int[,] numbers2D = new int[3, 2] { { 99, 909 }, { 7, 77 }, { 55, 505 } };
foreach (int i in numbers2D)
{
textBox2.Text += i.ToString() + Environment.NewLine;
}
}
运行结果如下;