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

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

相关推荐
EasyCVR2 分钟前
EasyRTC嵌入式音视频通话SDK:基于纯C语言的跨平台实时通信系统设计与实践
linux·c语言·开发语言·音视频·webrtc·h.265
qq_447663051 小时前
《Spring日志整合与注入技术:从入门到精通》
java·开发语言·后端·spring
蜡笔小新星2 小时前
OpenCV中文路径图片读写终极指南(Python实现)
开发语言·人工智能·python·opencv·计算机视觉
Rverdoser2 小时前
java八股文之消息中间件
c#·linq
七七知享2 小时前
2024 Qiniu 跨平台 Qt 高级开发全解析
开发语言·qt·零基础·操作系统·跨平台·qt5·精通
脏脏a2 小时前
C 语言分支与循环:构建程序逻辑的基石
c语言·开发语言
结衣结衣.2 小时前
【Qt】带参数的信号和槽函数
开发语言·qt·c++11
冷琴19962 小时前
基于Python+Vue开发的电影订票管理系统源码+运行步骤
开发语言·vue.js·python
L Jiawen2 小时前
【Python 2D绘图】Matplotlib绘图(统计图表)
开发语言·python·matplotlib
Run_Teenage3 小时前
C语言每日一练——day_4
c语言·开发语言