Convert Array into a primative method to express the same result, using no LINQ only int.

前端 未结 4 381
萌比男神i
萌比男神i 2021-01-27 10:39

my program records the number of bottles four rooms has collected in a bottle drive. When the user types in quit, the number of bottle each room has collected is shown as well a

4条回答
  •  旧巷少年郎
    2021-01-27 11:27

    Here's an example that uses a Class to hold the information for each room. The reason for using a class is so that if your program needs to change in the future to collect more information, you won't have to track yet another array, you can just add properties to the class.

    The individual rooms are now held in a list instead of an array just to show a different construct.

    Here is the new Room class:

    public class Room
    {
        public int Number { get; set; }
        public int BottleCount { get; set; }
    
        public Room(int wNumber)
        {
            Number = wNumber;
        }
    }
    

    And here is the new version of the program. Note that additional checking of the values entered by the end user has been added in order to prevent exceptions when trying to get the current room or parse to an int the value entered by the user:

        static void Main(string[] args)
        {
            const int MAX_ROOMS = 4;
            var cRooms = new System.Collections.Generic.List();
    
            for (int nI = 0; nI < MAX_ROOMS; nI++)
            {
                // The room number is 1 to 4
                cRooms.Add(new Room(nI + 1));
            }
    
            // Initializes the room that wins
            //Start of while loop to ask what room your adding into. 
            while (true)
            {
                Console.Write("Enter the room you're in: ");
                //If user enters quit at anytime, the code will jump out of while statement and enter for loop below
                string roomNumber = Console.ReadLine();
                if (roomNumber == "quit")
                {
                    //Break statement allows quit to jump out of loop
                    break;
                }
                int room = 0;
                if (int.TryParse(roomNumber, out room) && (room < MAX_ROOMS) && (room >= 0)) {
                    Room currentRoom;
    
                    currentRoom = cRooms[room];
    
                    Console.Write("Bottles collected in room {0}: ", currentRoom.Number);
    
                    int wBottleCount = 0;
    
                    if (int.TryParse(Console.ReadLine(), out wBottleCount) && (wBottleCount >= 0))
                    {
                        // This line adds the count of bottles and records it so you can continuously count the bottles collected.
                        currentRoom.BottleCount += wBottleCount;
                    }
                    else
                    {
                        Console.WriteLine("Invalid bottle count; value must be greater than 0");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid room number; value must be between 1 and " + MAX_ROOMS.ToString());
                }
            }
    
            Room maxRoom = null;
    
            foreach (Room currentRoom in cRooms) //This loop goes through the array of rooms (4)
            {
                // This assumes that the bottle count can never be decreased in a room
                if ((maxRoom == null) || (maxRoom.BottleCount < currentRoom.BottleCount))
                {
                    maxRoom = currentRoom;
                }
                Console.WriteLine("Bottles collected in room {0} = {1}", currentRoom.Number, currentRoom.BottleCount);
            }
            //Outputs winner
            Console.WriteLine("And the Winner is room " + maxRoom.Number + "!!!");
        }
    

提交回复
热议问题