在写一个数组并将零返回原位

时间:2014-02-04 12:01:36

标签: c#

我正在做一些学校工作,只想指出正确的方向。我正在创建一个酒店项目,该项目可以预订人员并在离开时预订。我在某个部分挣扎,询问你需要腾出哪个房间,然后在数组中将0放入这个位置。我不打算绕着灌木丛,我不知道从哪里开始。到目前为止,这是我的代码。

using System;

namespace Task7_2
{


    class Motel
    {
        int[] rooms;
        const int MAX = 21;
        int roomNumber, guests, vacate;

        static void Main()
        {
            Motel BatesMotel = new Motel();
            BatesMotel.runMotel();
            BatesMotel.showAllRooms();



        }
        //*******************************************************
        public Motel()
        {
            rooms = new int[MAX + 1];        // allow rooms from 1 to MAX
        }
        //******************************************************
        public void runMotel()
        {
            string choice = "";
            do
            {
                Console.Clear();
                Console.WriteLine("The Bates Motel");
                Console.WriteLine("===============");
                Console.WriteLine("1. Book a room");
                Console.WriteLine("2. Vacate a room");
                Console.WriteLine("3. Display ALL Room Details");
                Console.WriteLine("4. Vacate ALL rooms");
                Console.WriteLine("5. Quit");
                Console.Write("Enter your choice : ");
                choice = Console.ReadLine();
                if (choice == "1")
                {
                    bookRoom();
                }
                else if (choice == "3")
                {

                    showAllRooms();
                }
                else if (choice == "2")
                {
                    vacateOneRoom();
                }
            }
            while (choice != "5");
        }
        //*******************************************************
        public void bookRoom()
        {

            Console.WriteLine("\nThe Bates Motel");
            Console.WriteLine("===============");
            Console.WriteLine("Book a room");
            Console.Write("Enter the room number : ");
            roomNumber = Convert.ToInt32(Console.ReadLine());
            Console.Write("How many guests : ");
            guests = Convert.ToInt32(Console.ReadLine());
            rooms[roomNumber] = guests;     // make the booking
            Console.WriteLine("Room " + roomNumber + " booked for " + guests + " people");

        }
        //*******************************************************
        public void showAllRooms()
        {

            for (int i = 1; i < MAX; i++)
            {
                Console.Write("Room " + (i )+"\t\t\t" + rooms[i] + " guests \n" );
            }
            Console.ReadLine();


        }
        public void vacateOneRoom()
        {
              Console.WriteLine("Which room is being vacated");


              Console.ReadLine();
        }
      }
}

2 个答案:

答案 0 :(得分:1)

using System.Collections.Generic;

List<int> myList= new List<int>();

int num = 22;
myList.Add(num);
myList.Remove(num); //removes matching item

myList.Add(33);
myList.RemoveAt(0); //removes at array index

答案 1 :(得分:0)

bool[] barray = new bool[number_of_rooms];

当有人预订某个房间时

barray[room_number]=true;

当有人离开某个房间时,

barray[room_number]=false;

检查

for(int i=0;i<barray.lenght;i++)
{
if(barray[i]==true)
Console.WriteLine("Room number"+300+i+"is not free");
else
Console.WriteLine("Room number"+300+i+"is free");
}

示例输出:::

300号房间不是免费的 301号房间不是免费的 302号房间免费 303号房间不是免费的 304号房间免费