是否需要进行更改以按数字顺序而不是随机地生成此代码簿?

时间:2015-12-22 20:16:09

标签: c# arrays list random numerical

**我希望代码以数字顺序打印出来而不是像目前那样随机整数,因为我现在想要将预订从1填充到32的上限。任何帮助在此之前,我将非常感谢。

抱歉,如果我的英语不是最好的,那么它不是我的第一语言!**

 class FirstClassCompartment
 { 
        public void FirstClassTickets()
        {
            Random rand = new Random();
            bool[] seats = new bool[32];
            List<int> seatsBooked = new List<int>();
            int completeBooking = 0;
            bool quit = false;
            string ticketAmount = "";
            {
                Console.Clear();
                Console.WriteLine("\nWelcome to the First Class booking menu");
                Console.WriteLine("");
                Console.WriteLine("Please enter the amount of tickets you would like to book");
                ticketAmount = Console.ReadLine();
            }
            do
            {
                Console.Clear();
                Console.WriteLine("\nFirst Class booking menu");
                Console.WriteLine("");
                Console.WriteLine("Please press 1 to complete your first class booking");
                completeBooking = Int32.Parse(Console.ReadLine());

                switch (completeBooking)
                {
                    case 1:
                        int firstClassSeat;
                        if (seatsBooked.Count == 0)
                        {
                            firstClassSeat = rand.Next(32);
                            seats[firstClassSeat] = true;
                            seatsBooked.Add(firstClassSeat);
                        }
                        else
                        {

                            do
                            {
                                firstClassSeat = rand.Next(32);
                                if (!seatsBooked.Contains(firstClassSeat))
                                {
                                    seats[firstClassSeat] = true;

                                }
                            }
                            while (seatsBooked.Contains(firstClassSeat) && seatsBooked.Count < 32);
                            if (seatsBooked.Count < 32)
                            {
                                seatsBooked.Add(firstClassSeat);
                            }
                        }
                        if (seatsBooked.Count >= 32)
                        {
                            Console.WriteLine("All seats for first class have been booked");
                            Console.WriteLine("Please press enter to continue...");

                        }
                        else
                        {
                            Console.WriteLine("Your seat: {0}", firstClassSeat + 1);
                            Console.WriteLine("Please press enter to continue...");

                        }
                        Console.ReadLine();
                        break;
                    default:
                        Console.WriteLine("ERROR: INVALID SELECTION");
                        quit = true;
                        break;
                }
            } while (!quit);
        }
    }
}

强文

1 个答案:

答案 0 :(得分:0)

int firstClassSeat=0放在do {} while()循环之前,而不是在switch()语句中。然后,代替firstClassSeat=rand.Next(32);,只需执行firstClassSeat++;