1 text format
/// <summary>
/// Lesson 2 of "Xiaobai Learning Programs": Arrays and Sorting
/// </summary>
/// <param name="sender"></param>
/// <param name ="e"></param>
private void button2_Click(object sender, EventArgs e)
{ // #1 array: a set of data (member or unit) double[] score = { 540, 340, 650, 120, 554, 643, 612, 234, 345, 456 }; // Number of array members int n = 10;
// #2 Print array (sequential output array)
// Form a long string first, and then output it;
// StringBuilder is the most commonly used way to combine string data into regular strings;
StringBuilder sb = new StringBuilder( );
// AppendLine adds a line; <br> is a newline;
sb.AppendLine("Score before sorting: <br>");
// #3 Loop
// The sentence (statement) at the beginning of for becomes a loop, which is sequential processing;
// Detailed explanation of for statement: define a flag i, from 0 to n-1 = (before n); add 1 each time;
/ / Visible array flags (subscripts) start from 0! ! ! ! ! ! !
#region standard writing
for (int i = 0; i < n; i++)
{ sb.AppendLine((i + 1) + ": " + score[i] + "<br>"); } sb.AppendLine(" <br>"); #endregion
#region Another way of writing
#if __UNUSED__
// Detailed explanation of for statement: define a flag i, from 1 to n = (n+1); add 1 each time;
for (int i = 1; i < n + 1; i++)
{ sb. AppendLine(i + ": " + score[i - 1] + "<br>"); } sb. AppendLine("<br>"); #endif #endregion
// #4 Sorting
// Two-layer loop; a starts from the first one, and compares it with every subsequent b's grade;
// If a's grade exceeds b's grade's exchange;
for (int i = 0; i < n - 1; i++)
{ for (int j = i + 1; j < n; j++) { if (score[i] > score[j]) { // exchange; // need a pad (stemp) in the middle For temporary storage of grade a double temp = score[i]; score[i] = score[j]; score[j] = stemp; } } }
// #5 Output sorted results
sb.AppendLine("Scores after sorting from small to large: <br>");
for (int i = 1; i < n + 1; i++)
{ sb.AppendLine(i + ": " + score[i - 1] + "<br>"); } sb. AppendLine("<br>");
// #6 Output to an exported embedded web browser
webBrowser1.DocumentText = sb.ToString();
}
2 code format
/// <summary>
/// 《小白学程序》第二课:数组与排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
// #1 数组:一组数据(成员 或 单元)
double[] score = {
540, 340, 650, 120, 554,
643, 612, 234, 345, 456
};
// 数组成员的个数
int n = 10;
// #2 打印数组(顺序输出数组)
// 先组成长长的串,最后再输出;
// StringBuilder 是最常用的进行字符串 数据 组合成常常的字符串的方式;
StringBuilder sb = new StringBuilder();
// AppendLine 加一行;<br> 是换行;
sb.AppendLine("未排序之前的成绩:<br>");
// #3 循环
// for 开始的句子(语句)成为循环,就是顺序处理;
// for 语句详解:定义一个标志 i, 从 0 到 n-1 = (n之前);每次加 1 ;
// 可见数组的标志(下标)是从 0 开始的!!!!!!!
#region 标准写法
for (int i = 0; i < n; i++)
{
sb.AppendLine((i + 1) + ": " + score[i] + "<br>");
}
sb.AppendLine("<br>");
#endregion
#region 另一种写法
#if __UNUSED__
// for 语句详解:定义一个标志 i, 从 1 到 n = (n+1)之前;每次加 1 ;
for (int i = 1; i < n + 1; i++)
{
sb.AppendLine(i + ": " + score[i - 1] + "<br>");
}
sb.AppendLine("<br>");
#endif
#endregion
// #4 排序
// 两层循环;a 从第一个开始,每次与后面每一个 b 的成绩比较;
// 如果 a 成绩超过 b 成绩的交换;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (score[i] > score[j])
{
// 交换;
// 中间需要一块垫板(stemp)用于临时存放 a成绩
double stemp = score[i];
score[i] = score[j];
score[j] = stemp;
}
}
}
// #5 输出排序后的结果
sb.AppendLine("从小到大排序之后的成绩:<br>");
for (int i = 1; i < n + 1; i++)
{
sb.AppendLine(i + ": " + score[i - 1] + "<br>");
}
sb.AppendLine("<br>");
// #6 输出到一个出口嵌入的网页浏览器
webBrowser1.DocumentText = sb.ToString();
}
You are very lucky to read the only real programming tutorial in the world.