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

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

相关推荐
牙痛不能吃糖,哭2 分钟前
C++面试复习日记(8)2025.4.25,malloc,free和new,delete的区别
开发语言·c++
健康的猪6 分钟前
golang的cgo的一点小心得
开发语言·后端·golang
冰茶_10 分钟前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
夜夜敲码27 分钟前
C语言教程(十六): C 语言字符串详解
c语言·开发语言
宋康34 分钟前
C语言结构体和union内存对齐
c语言·开发语言
居然是阿宋44 分钟前
Kotlin高阶函数 vs Lambda表达式:关键区别与协作关系
android·开发语言·kotlin
Cao1234567893211 小时前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine1 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
亿坊电商1 小时前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
烁3471 小时前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法