有人能把这个数组代码变成一个非数组方法来获得同样的结果吗

本文关键字:数组 方法 一个 结果 代码 | 更新日期: 2023-09-27 18:20:30

我这里有一段代码,记录了4个房间收集的瓶子数量。当用户输入退出时,程序会吐出每个房间收集的瓶子数量,并确定收集瓶子数量最多的房间。我已经使用了一个数组方法,但对我来说,我不应该使用这个方法,只是为了展示数组有多有用。有人能给我什么建议吗?

namespace BottleDrive
  {
      class Program
    {
        static void Main(string[] args)
        {//Initialize array of rooms to 4
            int[] rooms = new int[4];
            //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 quit = Console.ReadLine();
                if (quit == "quit")
                    //Break statement allows quit to jump out of loop
                    break; 
               //Variable room holds the number of bottles collect by each room. 
                int room = int.Parse(quit);
                Console.Write("Bottles collected in room {0}: ", room);
                // This line adds the count of bottles and records it so you can continuously count the bottles collected.
                rooms[room - 1] += int.Parse(Console.ReadLine());                
            }
            //This for statement lists the 4 rooms and their bottle count when the user has entered quit. An alternative to below
            /*for (int i = 0; i < rooms.Length; ++i)
                Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);*/
            int maxValue = 0;//initiates the winner, contructor starts at 0
            int maxRoomNumber = 0;//initiates the room number that wins
            for (int i = 0; i < rooms.Length; ++i)//This loop goes through the array of rooms (4)
            {
                if (rooms[i] > maxValue)//Makes sure that the maxValue is picked in the array
                {//Looking for room number for the 
                    maxValue = rooms[i];
                    maxRoomNumber = i + 1;
                }//Writes the bottles collected by the different rooms
                Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);
            }
            //Outputs winner
            Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
        }
          }
            }

有人能把这个数组代码变成一个非数组方法来获得同样的结果吗

如果不允许使用数组,则可以为所有数组项声明4个变量。

int room1, room2, room3, room4;

我想这就是这个练习的预期方法。

下面是一个使用class保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必跟踪另一个数组,只需向类添加属性即可。

各个房间现在被保存在一个列表中,而不是一个数组中,只是为了显示不同的结构。

这是新的房间等级:

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

这是这个程序的新版本。请注意,添加了对最终用户输入的值的额外检查,以防止在尝试获取当前房间或将用户输入的数值解析为int时出现异常:

    static void Main(string[] args)
    {
        const int MAX_ROOMS = 4;
        var cRooms = new System.Collections.Generic.List<Room>();
        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 + "!!!");
    }