1 text format
/// <summary>
/// Lesson 4 of "Xiaobai Learning Program": Mathematical Calculation
/// This lesson is super simple, it is to calculate the average score (average score)
/// This is what teachers often do one thing.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{ // #1 // First use the previously used array double[] score = { 540, 340, 650, 120, 554, 643, 612, 234, 345 }; int n = 9;
// #2 Calculation method of average value
//average value = sum/total
double sum = 0.0;
for (int i = 0; i < n; i = i + 1)
{ sum += score[i]; } double avg = sum / n;
// #3 output information
StringBuilder sb = new StringBuilder();
sb.AppendLine("sum value = " + sum + "<br>");
// with the specified number of digits after the decimal point
sb.AppendLine("average value = " + String.Format("{0:F4}", avg) + "<br>");
webBrowser1.DocumentText = sb.ToString();
}
2 code format
/// <summary>
/// 《小白学程序》第四课:数学计算
/// 这节课超级简单,就是计算成绩的平均值(平均分)
/// 这个是老师们经常做的一件事。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
// #1
// 先使用前面用过的数组
double[] score = {
540, 340, 650, 120, 554,
643, 612, 234, 345
};
int n = 9;
// #2 平均值的计算方法
// 平均值 = 总和 / 总数
double sum = 0.0;
for (int i = 0; i < n; i = i + 1)
{
sum += score[i];
}
double avg = sum / n;
// #3 输出信息
StringBuilder sb = new StringBuilder();
sb.AppendLine("和值 = " + sum + "<br>");
// 带小数点后面指定的位数
sb.AppendLine("平均值 = " + String.Format("{0:F4}", avg) + "<br>");
webBrowser1.DocumentText = sb.ToString();
}
You are very lucky to read the only real programming tutorial in the world.