打印出人类可读的C#列表的内容 [英] Print out contents of a list human readable C#

查看:61
本文介绍了打印出人类可读的C#列表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to C# and I would like to print out the contents of a list so that the information stored in seatsBooked will be displayed for the user if they pick the number 3 case of my switch statement. My code is as follows:

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


    }
}

}

推荐答案

您可能已经发现Console.WriteLine(seatsBooked);不会向您显示列表的内容.一种实现方法是foreach循环.最简单的形式如下所示.

As you have probably discovered Console.WriteLine(seatsBooked); will not show you the contents of the list. One way to do that is a foreach loop. The simplest form would look like this.

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

这篇关于打印出人类可读的C#列表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆