瓶子计数程序,查询中的switch语句,查找最大数量

时间:2012-01-14 23:34:07

标签: c# int switch-statement max

我的程序记录了4个房间收集的瓶子数量。我是C#的初学者,但过去曾做过java。

我不会使用LINQ,我不会使用Arrays。仅限切换语句(抱歉,我知道效率低下)

我的程序必须记录用户输入的瓶子数量,当用户键入“退出”时,程序假定吐出所有房间的瓶数并确定最多瓶子的房间作为获胜者。

我停留在这个切换声明中,我无法找到启动房间的方法(room1,room2,room3,room4),它表示未分配变量room1-4。我应该能够通过使用开关不断地将瓶子添加到房间。

当我输入戒烟时,该程序可以吐出房间收集的所有瓶子,并找到装有最多瓶子的房间。

感谢您的时间,感谢社区为我提供了多少帮助。

 namespace BottleDrive1
 {
    class Program
    {
    static void Main(string[] args)
    {//Initialize 4 rooms. 
        int room1 = 0;

        int room2 = 0;

        int room3 = 0;

        int room4 = 0;
        //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;}}
            private void SetRoom(int room, int value)
            {
            switch (room)
            {
                case 1:
                    room1 = value;
                    break;
                case 2:
                    room2 = value;
                    break;
                case 3:
                    room3 = value;
                    break;
                case 4:
                    room4 = value;
                    break;
            }
            }
            public int GetRoom(int room)
            {
                int count = int.Parse(Console.ReadLine());
                switch (room)
                {
                    case 1:
                        room1 += count;
                        break;
                    case 2:
                        room2 += count;
                    case 3:
                        room3 += count;
                        break;
                    case 4:
                        room4 += count;
                        break;
                }

            }
        }
        //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 < room[i].Length; ++i)//This loop goes through the array of rooms (4)
        {
            if (room[i] > maxValue)//Makes sure that the maxValue is picked in the array
            {//Looking for room number for the  
                maxValue = room[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 + "!!!");
        */
    }

该程序的最后一部分是我试图找到最大数量,因为我使用了一个数组来启动。我不能使用数组。

2 个答案:

答案 0 :(得分:1)

将int声明移到Main方法上方,如下所示:

class Program
{
     int room1 = 0;
     ....
    static void Main(string[] args)
    {
    ....
    }
}

答案 1 :(得分:0)

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

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

这是新的Room类:

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 + "!!!");
    }