二维数组练习

            //创建一个控制台应用程序,使用二维数组存储火车票信息,输入车次和姓名后,模拟预订火车票功能,代码如下:
            string train = "", destination = "", StartTime = "";//声明3个字符串: 车次,车次信息, 出发时间;
            string[] 标题 = { "车次", "出发站-到达站", "出发时间", "到达时间", "历时" };//声明标题;
            //定义二维数组, 存储车次信息;
            string[,] tableValue = {{ "T40","长春-北京","00:12","\t12:20","\t12:08"},
                                    {"T298","长春-北京","00:06","\t10:50","\t10:44" },
                                    {"Z158","长春-北京","12:48","\t21:06","\t08:18" },
                                    {"K1084","长春-北京","12:39","\t02:16","\t13:37" }};
            //遍历一维数组, 用来输出标题:
            for (int i = 0; i < 标题.Length; i++)
            {
                Console.Write(标题[i] + "\t");
            }
            //输入车次信息:
            string messages = "";//用来存储获取来的车次信息;
            Console.WriteLine();//换行

            for (int i = 0; i < tableValue.GetLength(0); i++)
            {
                for (int j = 0; j < tableValue.GetLength(1); j++)
                {
                    Console.Write(tableValue[i, j] + "\t");
                }
                train = tableValue[i, 0];
                destination = tableValue[i, 1];
                StartTime =tableValue[i, 2];
                messages += train + "列车"+" "+ destination +" "+ StartTime+" " + "" + ",";
                Console.WriteLine();
            }
            Console.WriteLine("请输入乘坐车次:");
            string ticket = Console.ReadLine();
            string[] message = messages.Split(',');
            for (int i = 0; i < message.Length-1; i++)//message长度比tableValue的1维长度多1;
            {
                //if (message[i].Contains(ticket))//判断是否有输入的车次;
                if (tableValue[i,0]==ticket)//判断是否有输入的车次;
                {
                    Console.WriteLine("请输入您的姓名:");
                    string name = Console.ReadLine();
                    Console.WriteLine(name+"您好,您购买的"+ message[i]+";");

                }
            }
            Console.ReadLine();

 

posted @ 2024-08-25 14:26  CSF践行  阅读(4)  评论(0编辑  收藏  举报