瓶子计数程序,切换报表查询,找到最大数量

本文关键字:查询 最大数 报表 程序 瓶子 | 更新日期: 2023-09-27 18:20:31

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

我不会使用LINQ,我不会使用Arrays。仅切换语句(对不起,我知道它效率低下)

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

我被这个开关语句卡住了,我找不到启动我的房间(房间1、房间2、房间3、房间4)的方法,它说变量房间1-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 + "!!!");
        */
    }

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

瓶子计数程序,切换报表查询,找到最大数量

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

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

下面是一个使用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 + "!!!");
    }