C#,《小白学程序》第七课:列表(List)应用之一“编制高铁车次信息表”

1 文本格式

/// <summary>

/// 车站信息类 class

/// </summary>

public class Station

{

/// <summary>

/// 编号

/// </summary>

public int Id { get; set; } = 0;

/// <summary>

/// 车站名

/// </summary>

public string Name { get; set; } = string.Empty;

public Station(int id, string name)

{

this.Id = id;

this.Name = name;

}

}

// 列表的初值

List<Station> stations = new List<Station>() {

new Station(1,"北京"),

new Station(2,"石家庄"),

new Station(3,"香河"),

new Station(4,"唐山"),

new Station(5,"北戴河"),

new Station(6,"秦皇岛"),

new Station(7,"廊坊"),

new Station(8,"天津"),

};

/// <summary>

/// 《小白学程序》第七课:列表(List)应用之二------------编制高铁车次信息表

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button7_Click(object sender, EventArgs e)

{

// #1 创建列表(列车车次信息)

List<Station> G103 = new List<Station>();

// #2 添加节点(车站信息)

G103.Add(stations[0]);

G103.Add(stations[2]);

G103.Add(stations[3]);

G103.Add(stations[4]);

G103.Add(stations[5]);

// #3 输出车次信息

StringBuilder sb = new StringBuilder();

sb.AppendLine("1 去程(正向)<br>");

foreach (Station s in G103)

{

sb.AppendLine(s.Id + " " + s.Name + "<br>");

}

sb.AppendLine("<br>");

// #4 返程(列表的反向)

G103.Reverse();

List<Station> G104 = G103;

sb.AppendLine("2 返程(反向)<br>");

foreach (Station s in G103)

{

sb.AppendLine(s.Id + " " + s.Name + "<br>");

}

webBrowser1.DocumentText = sb.ToString();

}

2 代码格式

cs 复制代码
/// <summary>
/// 车站信息类 class
/// </summary>
public class Station
{
    /// <summary>
    /// 编号
    /// </summary>
    public int Id { get; set; } = 0;
    /// <summary>
    /// 车站名
    /// </summary>
    public string Name { get; set; } = string.Empty;

    public Station(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

// 列表的初值
List<Station> stations = new List<Station>() {
    new Station(1,"北京"),
    new Station(2,"石家庄"),
    new Station(3,"香河"),
    new Station(4,"唐山"),
    new Station(5,"北戴河"),
    new Station(6,"秦皇岛"),
    new Station(7,"廊坊"),
    new Station(8,"天津"),
};

/// <summary>
/// 《小白学程序》第七课:列表(List)应用之二------------编制高铁车次信息表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
    // #1 创建列表(列车车次信息)
    List<Station> G103 = new List<Station>();

    // #2 添加节点(车站信息)
    G103.Add(stations[0]);
    G103.Add(stations[2]);
    G103.Add(stations[3]);
    G103.Add(stations[4]);
    G103.Add(stations[5]);

    // #3 输出车次信息
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("1 去程(正向)<br>");
    foreach (Station s in G103)
    {
        sb.AppendLine(s.Id + " " + s.Name + "<br>");
    }
    sb.AppendLine("<br>");

    // #4 返程(列表的反向)
    G103.Reverse();
    List<Station> G104 = G103;

    sb.AppendLine("2 返程(反向)<br>");
    foreach (Station s in G103)
    {
        sb.AppendLine(s.Id + " " + s.Name + "<br>");
    }


    webBrowser1.DocumentText = sb.ToString();
}

你非常幸运读到的是第一本真正的程序教程。

相关推荐
shanshan2099几秒前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
三玖诶3 分钟前
如何在 Qt 的 QListWidget 中为某一行添加点击事件
开发语言·qt
InJre9 分钟前
QT widgets 窗口缩放,自适应窗口大小进行布局
开发语言·qt·ui
可愛小吉12 分钟前
Python 课程10-单元测试
开发语言·python·单元测试·tdd·unittest
五味香24 分钟前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆25 分钟前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
爱棋笑谦32 分钟前
二叉树计算
java·开发语言·数据结构·算法·华为od·面试
翔云API1 小时前
人证合一接口:智能化身份认证的最佳选择
大数据·开发语言·node.js·ocr·php
jimmy.hua1 小时前
C++刷怪笼(5)内存管理
开发语言·数据结构·c++
xiaobai12 31 小时前
二叉树的遍历【C++】
开发语言·c++·算法