首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >瓶子计数程序,switch语句inquery,查找最大数量

瓶子计数程序,switch语句inquery,查找最大数量
EN

Stack Overflow用户
提问于 2012-01-15 07:34:07
回答 2查看 724关注 0票数 0

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

我不会使用LINQ,我不会使用数组。只有Switch语句(对不起,我知道它的效率很低)

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

我被这个switch语句卡住了,我找不到一种方法来初始化我的房间(room1,room2,room3,room4),它说变量room1-4没有赋值。我应该能够通过使用开关不断地将瓶子添加到房间中。

当我键入quit时,程序会列出房间收集的所有瓶子,并找到瓶子最多的房间。

感谢您的时间,我感谢这个社区对我的帮助。

代码语言:javascript
复制
 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 + "!!!");
        */
    }

程序的最后一部分是我试图找到最大的数字,因为我使用了一个数组开始。我不能使用数组。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-15 11:01:26

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

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

下面是新的Room类:

代码语言:javascript
复制
public class Room
{
    public int Number { get; set; }
    public int BottleCount { get; set; }

    public Room(int wNumber)
    {
        Number = wNumber;
    }
}

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

代码语言:javascript
复制
    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 + "!!!");
    }
票数 0
EN

Stack Overflow用户

发布于 2012-01-15 07:51:25

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

代码语言:javascript
复制
class Program
{
     int room1 = 0;
     ....
    static void Main(string[] args)
    {
    ....
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8866304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档