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();
}

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

相关推荐
CodeWithMe18 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found28 分钟前
Day46 Python打卡训练营
开发语言·python
love530love30 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月31 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华235 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen37 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰37 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心38 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程42 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
q567315231 小时前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫