打印出人类可读C#列表的内容

时间:2014-09-10 01:43:45

标签: c# list

我是C#的新手,我想打印出列表的内容,以便当用户选择我的switch语句的3号情况时,将为用户显示存储在seatBooked中的信息。我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AirlineReservation

{
class Program
{
    static void Main(string[] args)
    {
        Random rand = new Random();
        bool[] seats = new bool[10];
        //To keep a separate list of seats taken          
        List<int> seatsBooked = new List<int>();
        int inputI = 0;
        char inputC = ' ';
        bool quit = false;
        do
        {
            Console.Clear();
            int assignSeat = rand.Next(5, 10);
            Console.WriteLine("Thanks for flying with Steve-O Airlines" + "\n" + '\n' +
                             "\nPlease enter the number one [1] for First Class" +
                             " \nPlease enter the number two [2] for Economy" +
                             "\nPlease enter the number three [3] for seats taken" +
                              "\nPlease enter the number four [4] to exit the order system");
            inputI = Int32.Parse(Console.ReadLine());
            switch (inputI)
            {
                case 1: //is the seat booked, if not book it
                    int assignedSeat;
                    if (seatsBooked.Count == 0) 
                    {
                        assignedSeat = rand.Next(0, 5);
                        seats[assignedSeat] = true;
                        seatsBooked.Add(assignedSeat);                            
                    }
                    else
                    {

                        do //while there are available seats and current seat has not being assigned before.
                        {
                            assignedSeat = rand.Next(0, 5);
                            if (!seatsBooked.Contains(assignedSeat)) //if assignedSeat is not booked.
                            {
                                seats[assignedSeat] = true;

                            }
                            //repeat while the random seat number is already booked and there are  avaialable seats
                        } while (seatsBooked.Contains(assignedSeat) && seatsBooked.Count < 5);

                        if (seatsBooked.Count < 5) //if seatsBooked list is not full for First Class
                        {
                            seatsBooked.Add(assignedSeat); //Add current random-generated seat to the list.
                        }

                    }

                    if (seatsBooked.Count >= 5)
                    {
                        Console.WriteLine("All seats for First Class are booked! Looks like a bad lunch for you.");
                        Console.WriteLine("Press enter to continue...");

                    }
                    else
                    {
                        Console.WriteLine("Your seat number is: {0}" + " \nNow pay me $550", assignedSeat + 1);
                        Console.WriteLine("Press enter to continue...");

                    }
                    Console.ReadLine();
                    break;
                case 2:
                    seats[assignSeat] = true;
                    Console.WriteLine("Your seat number is: {0}"+ " \nNow pay me $350", assignSeat + 1);
                    Console.WriteLine("Press enter to continue...");
                    Console.ReadLine();
                    break;
                case 3:

                        Console.WriteLine(seatsBooked);  



                    break;
                case 4:
                    quit = true;
                    break;
                default:
                    Console.WriteLine("ERROR::INVALID SELECTION" +
                        "\nYou will not get a flight this way!" );
                    quit = true;
                    break;
            }
        } while (!quit);


    }
}

}

2 个答案:

答案 0 :(得分:3)

您可能已发现Console.WriteLine(seatsBooked);不会向您显示列表的内容。一种方法是foreach循环。最简单的形式看起来像这样。

foreach(int seatBooked in seatsBooked) {
    Console.WriteLine(seatBooked);
}

答案 1 :(得分:3)

你不能只打印出那样的列表......最终发生的事情是ToString()在你的收藏中被调用,你获得了你的班级的完全限定名称而不是数字列表。

但是,您可以创建一个可以正确打印的字符串。这样的事情应该有效:

Console.WriteLine(string.Join(", ", seatsBooked));  // concatenate elements with comma

如果您的列表包含数字1到5,那么您的输出将是:

  

1,2,3,4,5